| 1 | use ron::{ |
| 2 | de::from_str, |
| 3 | error::{Error, Position, Span, SpannedError}, |
| 4 | }; |
| 5 | |
| 6 | #[test] |
| 7 | fn test_hex() { |
| 8 | assert_eq!(from_str("0x507"), Ok(0x507)); |
| 9 | assert_eq!(from_str("0x1A5"), Ok(0x1A5)); |
| 10 | assert_eq!(from_str("0x53C537"), Ok(0x53C537)); |
| 11 | |
| 12 | assert_eq!( |
| 13 | from_str::<u8>("0x"), |
| 14 | Err(SpannedError { |
| 15 | code: Error::ExpectedInteger, |
| 16 | span: Span { |
| 17 | start: Position { line: 1, col: 1 }, |
| 18 | end: Position { line: 1, col: 3 }, |
| 19 | } |
| 20 | }) |
| 21 | ); |
| 22 | assert_eq!( |
| 23 | from_str::<u8>("0x_1"), |
| 24 | Err(SpannedError { |
| 25 | code: Error::UnderscoreAtBeginning, |
| 26 | span: Span { |
| 27 | start: Position { line: 1, col: 1 }, |
| 28 | end: Position { line: 1, col: 3 }, |
| 29 | } |
| 30 | }) |
| 31 | ); |
| 32 | assert_eq!( |
| 33 | from_str::<u8>("0xFFF"), |
| 34 | Err(SpannedError { |
| 35 | code: Error::IntegerOutOfBounds, |
| 36 | span: Span { |
| 37 | start: Position { line: 1, col: 3 }, |
| 38 | end: Position { line: 1, col: 6 }, |
| 39 | } |
| 40 | }) |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | #[test] |
| 45 | fn test_bin() { |
| 46 | assert_eq!(from_str("0b101"), Ok(0b101)); |
| 47 | assert_eq!(from_str("0b001"), Ok(0b001)); |
| 48 | assert_eq!(from_str("0b100100"), Ok(0b100100)); |
| 49 | |
| 50 | assert_eq!( |
| 51 | from_str::<u8>("0b"), |
| 52 | Err(SpannedError { |
| 53 | code: Error::ExpectedInteger, |
| 54 | span: Span { |
| 55 | start: Position { line: 1, col: 1 }, |
| 56 | end: Position { line: 1, col: 3 }, |
| 57 | } |
| 58 | }) |
| 59 | ); |
| 60 | assert_eq!( |
| 61 | from_str::<u8>("0b_1"), |
| 62 | Err(SpannedError { |
| 63 | code: Error::UnderscoreAtBeginning, |
| 64 | span: Span { |
| 65 | start: Position { line: 1, col: 1 }, |
| 66 | end: Position { line: 1, col: 3 }, |
| 67 | } |
| 68 | }) |
| 69 | ); |
| 70 | assert_eq!( |
| 71 | from_str::<u8>("0b111111111"), |
| 72 | Err(SpannedError { |
| 73 | code: Error::IntegerOutOfBounds, |
| 74 | span: Span { |
| 75 | start: Position { line: 1, col: 3 }, |
| 76 | end: Position { line: 1, col: 12 }, |
| 77 | } |
| 78 | }) |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | #[test] |
| 83 | fn test_oct() { |
| 84 | assert_eq!(from_str("0o1461"), Ok(0o1461)); |
| 85 | assert_eq!(from_str("0o051"), Ok(0o051)); |
| 86 | assert_eq!(from_str("0o150700"), Ok(0o150700)); |
| 87 | |
| 88 | assert_eq!( |
| 89 | from_str::<u8>("0o"), |
| 90 | Err(SpannedError { |
| 91 | code: Error::ExpectedInteger, |
| 92 | span: Span { |
| 93 | start: Position { line: 1, col: 1 }, |
| 94 | end: Position { line: 1, col: 3 }, |
| 95 | } |
| 96 | }) |
| 97 | ); |
| 98 | assert_eq!( |
| 99 | from_str::<u8>("0o_1"), |
| 100 | Err(SpannedError { |
| 101 | code: Error::UnderscoreAtBeginning, |
| 102 | span: Span { |
| 103 | start: Position { line: 1, col: 1 }, |
| 104 | end: Position { line: 1, col: 3 }, |
| 105 | } |
| 106 | }) |
| 107 | ); |
| 108 | assert_eq!( |
| 109 | from_str::<u8>("0o77777"), |
| 110 | Err(SpannedError { |
| 111 | code: Error::IntegerOutOfBounds, |
| 112 | span: Span { |
| 113 | start: Position { line: 1, col: 3 }, |
| 114 | end: Position { line: 1, col: 8 }, |
| 115 | } |
| 116 | }) |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | #[test] |
| 121 | fn test_dec() { |
| 122 | assert_eq!(from_str("1461"), Ok(1461)); |
| 123 | assert_eq!(from_str("51"), Ok(51)); |
| 124 | assert_eq!(from_str("150700"), Ok(150700)); |
| 125 | |
| 126 | assert_eq!( |
| 127 | from_str::<i8>("-_1"), |
| 128 | Err(SpannedError { |
| 129 | code: Error::UnderscoreAtBeginning, |
| 130 | span: Span { |
| 131 | start: Position { line: 1, col: 1 }, |
| 132 | end: Position { line: 1, col: 2 }, |
| 133 | } |
| 134 | }) |
| 135 | ); |
| 136 | assert_eq!( |
| 137 | from_str::<u8>("256"), |
| 138 | Err(SpannedError { |
| 139 | code: Error::IntegerOutOfBounds, |
| 140 | span: Span { |
| 141 | start: Position { line: 1, col: 1 }, |
| 142 | end: Position { line: 1, col: 4 }, |
| 143 | } |
| 144 | }) |
| 145 | ); |
| 146 | } |