github.com/aavshr/aws-sdk-go@v1.41.3/internal/ini/value_util_test.go (about) 1 //go:build go1.7 2 // +build go1.7 3 4 package ini 5 6 import ( 7 "testing" 8 ) 9 10 func TestStringValue(t *testing.T) { 11 cases := []struct { 12 b []rune 13 expectedRead int 14 expectedError bool 15 expectedValue string 16 }{ 17 { 18 b: []rune(`"foo"`), 19 expectedRead: 5, 20 expectedValue: `"foo"`, 21 }, 22 { 23 b: []rune(`"123 !$_ 456 abc"`), 24 expectedRead: 17, 25 expectedValue: `"123 !$_ 456 abc"`, 26 }, 27 { 28 b: []rune("foo"), 29 expectedError: true, 30 }, 31 { 32 b: []rune(` "foo"`), 33 expectedError: true, 34 }, 35 } 36 37 for i, c := range cases { 38 n, err := getStringValue(c.b) 39 40 if e, a := c.expectedValue, string(c.b[:n]); e != a { 41 t.Errorf("%d: expected %v, but received %v", i, e, a) 42 } 43 44 if e, a := c.expectedRead, n; e != a { 45 t.Errorf("%d: expected %v, but received %v", i, e, a) 46 } 47 48 if e, a := c.expectedError, err != nil; e != a { 49 t.Errorf("%d: expected %v, but received %v", i, e, a) 50 } 51 } 52 } 53 54 func TestBoolValue(t *testing.T) { 55 cases := []struct { 56 b []rune 57 expectedRead int 58 expectedError bool 59 expectedValue string 60 }{ 61 { 62 b: []rune("true"), 63 expectedRead: 4, 64 expectedValue: "true", 65 }, 66 { 67 b: []rune("false"), 68 expectedRead: 5, 69 expectedValue: "false", 70 }, 71 { 72 b: []rune(`"false"`), 73 expectedError: true, 74 }, 75 } 76 77 for _, c := range cases { 78 n, err := getBoolValue(c.b) 79 80 if e, a := c.expectedValue, string(c.b[:n]); e != a { 81 t.Errorf("expected %v, but received %v", e, a) 82 } 83 84 if e, a := c.expectedRead, n; e != a { 85 t.Errorf("expected %v, but received %v", e, a) 86 } 87 88 if e, a := c.expectedError, err != nil; e != a { 89 t.Errorf("expected %v, but received %v", e, a) 90 } 91 } 92 } 93 94 func TestNumericalValue(t *testing.T) { 95 cases := []struct { 96 b []rune 97 expectedRead int 98 expectedError bool 99 expectedValue string 100 expectedBase int 101 }{ 102 { 103 b: []rune("1.2"), 104 expectedRead: 3, 105 expectedValue: "1.2", 106 expectedBase: 10, 107 }, 108 { 109 b: []rune("123"), 110 expectedRead: 3, 111 expectedValue: "123", 112 expectedBase: 10, 113 }, 114 { 115 b: []rune("0x123A"), 116 expectedRead: 6, 117 expectedValue: "0x123A", 118 expectedBase: 16, 119 }, 120 { 121 b: []rune("0b101"), 122 expectedRead: 5, 123 expectedValue: "0b101", 124 expectedBase: 2, 125 }, 126 { 127 b: []rune("0o7"), 128 expectedRead: 3, 129 expectedValue: "0o7", 130 expectedBase: 8, 131 }, 132 { 133 b: []rune(`"123"`), 134 expectedError: true, 135 }, 136 { 137 b: []rune("0xo123"), 138 expectedError: true, 139 }, 140 { 141 b: []rune("123A"), 142 expectedError: true, 143 }, 144 } 145 146 for i, c := range cases { 147 base, n, err := getNumericalValue(c.b) 148 149 if e, a := c.expectedValue, string(c.b[:n]); e != a { 150 t.Errorf("%d: expected %v, but received %v", i+1, e, a) 151 } 152 153 if e, a := c.expectedRead, n; e != a { 154 t.Errorf("%d: expected %v, but received %v", i+1, e, a) 155 } 156 157 if e, a := c.expectedError, err != nil; e != a { 158 t.Errorf("%d: expected %v, but received %v", i+1, e, a) 159 } 160 161 if e, a := c.expectedBase, base; e != a { 162 t.Errorf("%d: expected %d, but received %d", i+1, e, a) 163 } 164 } 165 }