github.com/searKing/golang/go@v1.2.117/go/scanner/scanner_test.go (about) 1 // Copyright 2020 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package scanner_test 6 7 import ( 8 "bufio" 9 "bytes" 10 "go/token" 11 "testing" 12 "unicode" 13 14 "github.com/searKing/golang/go/go/scanner" 15 ) 16 17 type ScanTests struct { 18 split bufio.SplitFunc 19 input []byte // input is the input that we want to tokenize. 20 output [][]byte 21 } 22 23 var ( 24 scanTests = []ScanTests{ 25 { 26 split: scanner.ScanWords, 27 input: []byte("cos(x) + 1i*sin(x) // Euler"), 28 output: [][]byte{[]byte("cos(x)"), []byte("+"), []byte("1i*sin(x)"), []byte("//"), []byte("Euler")}, 29 }, 30 { 31 split: scanner.ScanBytes, 32 input: []byte("cos(x) + 1i*sin(x) // Euler"), 33 output: [][]byte{[]byte("c"), []byte("o"), []byte("s"), []byte("("), []byte("x"), []byte(")"), 34 []byte(" "), []byte("+"), []byte(" "), 35 []byte("1"), []byte("i"), []byte("*"), []byte("s"), []byte("i"), []byte("n"), []byte("("), []byte("x"), []byte(")"), 36 []byte(" "), 37 []byte("/"), []byte("/"), 38 []byte(" "), 39 []byte("E"), []byte("u"), []byte("l"), []byte("e"), []byte("r")}, 40 }, 41 { 42 split: scanner.ScanLines, 43 input: []byte("cos(x) + 1i*sin(x) \n// Euler"), 44 output: [][]byte{[]byte("cos(x) + 1i*sin(x) "), []byte("// Euler")}, 45 }, 46 { 47 split: scanner.ScanRunes, 48 input: []byte("cos(x) + 1i*sin(x) // Euler"), 49 output: [][]byte{[]byte("c"), []byte("o"), []byte("s"), []byte("("), []byte("x"), []byte(")"), 50 []byte(" "), []byte("+"), []byte(" "), 51 []byte("1"), []byte("i"), []byte("*"), []byte("s"), []byte("i"), []byte("n"), []byte("("), []byte("x"), []byte(")"), 52 []byte(" "), 53 []byte("/"), []byte("/"), 54 []byte(" "), 55 []byte("E"), []byte("u"), []byte("l"), []byte("e"), []byte("r")}, 56 }, 57 { 58 split: scanner.ScanEscapes('"'), 59 input: []byte(`\\\r\n\"cos(x) + 1i*sin(x) // Euler`), 60 output: [][]byte{[]byte(`\\`), []byte(`\r`), []byte(`\n`), []byte(`\"`)}, 61 }, 62 { 63 split: scanner.ScanEscapes('"'), 64 input: []byte(`\y`), 65 output: [][]byte{}, 66 }, 67 { 68 split: scanner.ScanEscapes('"'), 69 input: []byte(`\xGGGG`), 70 output: [][]byte{}, 71 }, 72 { 73 split: scanner.ScanInterpretedStrings, 74 input: []byte(`\xGGGG`), 75 output: [][]byte{}, 76 }, 77 { 78 split: scanner.ScanInterpretedStrings, 79 input: []byte(`"""Hello World\""\xGGGG`), 80 output: [][]byte{[]byte(`""`), []byte(`"Hello World\""`)}, 81 }, 82 { 83 split: scanner.ScanRawStrings, 84 input: []byte("```Hello World\r`"), 85 output: [][]byte{[]byte("``"), []byte("`Hello World\r`")}, 86 }, 87 { 88 split: scanner.ScanMantissas(10), 89 input: []byte("123"), 90 output: [][]byte{[]byte("123")}, 91 }, 92 { 93 split: scanner.ScanMantissas(8), 94 input: []byte("1238"), 95 output: [][]byte{[]byte("123")}, 96 }, 97 { 98 split: scanner.ScanNumbers, 99 input: []byte("123"), 100 output: [][]byte{[]byte("123")}, 101 }, 102 { 103 split: scanner.ScanNumbers, 104 input: []byte("078"), 105 output: [][]byte{}, 106 }, 107 { 108 split: scanner.ScanNumbers, 109 input: []byte("123.56"), 110 output: [][]byte{[]byte("123.56")}, 111 }, 112 { 113 split: scanner.ScanNumbers, 114 input: []byte("123.56i78.96i"), 115 output: [][]byte{[]byte("123.56i"), []byte("78.96i")}, 116 }, 117 { 118 split: scanner.ScanNumbers, 119 input: []byte("0x55.FF"), 120 output: [][]byte{[]byte("0x55")}, 121 }, 122 { 123 split: scanner.ScanNumbers, 124 input: []byte("077x"), 125 output: [][]byte{[]byte("077")}, 126 }, 127 { 128 split: scanner.ScanNumbers, 129 input: []byte("0xFG"), 130 output: [][]byte{[]byte("0xF")}, 131 }, 132 { 133 split: scanner.ScanIdentifier, 134 input: []byte("_HelloWorld_&"), 135 output: [][]byte{[]byte("_HelloWorld_")}, 136 }, 137 { 138 split: scanner.ScanIdentifier, 139 input: []byte("_Hello World_&"), 140 output: [][]byte{[]byte("_Hello")}, 141 }, 142 { 143 split: scanner.ScanIdentifier, 144 input: []byte("_你好_&"), 145 output: [][]byte{[]byte("_你好_")}, 146 }, 147 { 148 split: scanner.ScanIdentifier, 149 input: []byte("0你好_&"), 150 output: [][]byte{}, 151 }, 152 { 153 split: scanner.ScanIdentifier, 154 input: []byte("H"), 155 output: [][]byte{[]byte("H")}, 156 }, 157 { 158 split: scanner.ScanUntil(unicode.IsSpace), 159 input: []byte("Hello World"), 160 output: [][]byte{[]byte("Hello")}, 161 }, 162 { 163 split: scanner.ScanWhile(unicode.IsSpace), 164 input: []byte(" \t Hello World"), 165 output: [][]byte{[]byte(" \t ")}, 166 }, 167 { 168 split: scanner.ScanRegexpPosix(`^Hello.*[ W]`), 169 input: []byte("Hello World"), 170 output: [][]byte{[]byte("Hello W")}, 171 }, 172 { 173 split: scanner.ScanRegexpPerl(`^Hello.*[ W]`), 174 input: []byte("Hello World"), 175 output: [][]byte{[]byte("Hello W")}, 176 }, 177 } 178 ) 179 180 func TestScan(t *testing.T) { 181 Outer: 182 for n, test := range scanTests { 183 // Initialize the scanner. 184 var s scanner.Scanner 185 fset := token.NewFileSet() // positions are relative to fset 186 file := fset.AddFile("", fset.Base(), len(test.input)) // register input "file" 187 s.Init(file, test.input, nil /* no error handler */, scanner.ModeCaseSensitive) 188 var i int 189 for ; ; i++ { 190 token, has := s.ScanSplits(test.split) 191 if !has { 192 break 193 } 194 if i >= len(test.output) { 195 t.Errorf("#%d: scan[%d] got %v; nothing expected", n, i, string(token)) 196 continue Outer 197 } 198 if bytes.Compare(token, test.output[i]) != 0 { 199 t.Errorf("#%d: scan[%d] got %v; expected %v", n, i, string(token), string(test.output[i])) 200 continue Outer 201 } 202 } 203 if i != len(test.output) { 204 t.Errorf("#%d: %v: got %d runs; expected %d", n, string(test.input), i, len(test.output)) 205 continue 206 } 207 } 208 }