github.com/sereiner/library@v0.0.0-20200518095232-1fa3e640cc5f/influxdb/escape/strings_test.go (about) 1 package escape 2 3 import ( 4 "testing" 5 ) 6 7 var s string 8 9 func BenchmarkStringEscapeNoEscapes(b *testing.B) { 10 for n := 0; n < b.N; n++ { 11 s = String("no_escapes") 12 } 13 } 14 15 func BenchmarkStringUnescapeNoEscapes(b *testing.B) { 16 for n := 0; n < b.N; n++ { 17 s = UnescapeString("no_escapes") 18 } 19 } 20 21 func BenchmarkManyStringEscape(b *testing.B) { 22 tests := []string{ 23 "this is my special string", 24 "a field w=i th == tons of escapes", 25 "some,commas,here", 26 } 27 28 for n := 0; n < b.N; n++ { 29 for _, test := range tests { 30 s = String(test) 31 } 32 } 33 } 34 35 func BenchmarkManyStringUnescape(b *testing.B) { 36 tests := []string{ 37 `this\ is\ my\ special\ string`, 38 `a\ field\ w\=i\ th\ \=\=\ tons\ of\ escapes`, 39 `some\,commas\,here`, 40 } 41 42 for n := 0; n < b.N; n++ { 43 for _, test := range tests { 44 s = UnescapeString(test) 45 } 46 } 47 } 48 49 func TestStringEscape(t *testing.T) { 50 tests := []struct { 51 in string 52 expected string 53 }{ 54 { 55 in: "", 56 expected: "", 57 }, 58 { 59 in: "this is my special string", 60 expected: `this\ is\ my\ special\ string`, 61 }, 62 { 63 in: "a field w=i th == tons of escapes", 64 expected: `a\ field\ w\=i\ th\ \=\=\ tons\ of\ escapes`, 65 }, 66 { 67 in: "no_escapes", 68 expected: "no_escapes", 69 }, 70 { 71 in: "some,commas,here", 72 expected: `some\,commas\,here`, 73 }, 74 } 75 76 for _, test := range tests { 77 if test.expected != String(test.in) { 78 t.Errorf("Got %s, expected %s", String(test.in), test.expected) 79 } 80 } 81 } 82 83 func TestStringUnescape(t *testing.T) { 84 tests := []struct { 85 in string 86 expected string 87 }{ 88 { 89 in: "", 90 expected: "", 91 }, 92 { 93 in: `this\ is\ my\ special\ string`, 94 expected: "this is my special string", 95 }, 96 { 97 in: `a\ field\ w\=i\ th\ \=\=\ tons\ of\ escapes`, 98 expected: "a field w=i th == tons of escapes", 99 }, 100 { 101 in: "no_escapes", 102 expected: "no_escapes", 103 }, 104 { 105 in: `some\,commas\,here`, 106 expected: "some,commas,here", 107 }, 108 } 109 110 for _, test := range tests { 111 if test.expected != UnescapeString(test.in) { 112 t.Errorf("Got %s, expected %s", UnescapeString(test.in), test.expected) 113 } 114 } 115 }