|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2023 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-cli. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to [email protected]. |
| 15 | + |
| 16 | +package cpp_test |
| 17 | + |
| 18 | +import ( |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/arduino/arduino-cli/arduino/builder/cpp" |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | +) |
| 24 | + |
| 25 | +func TestParseString(t *testing.T) { |
| 26 | + _, _, ok := cpp.ParseString(`foo`) |
| 27 | + require.Equal(t, false, ok) |
| 28 | + |
| 29 | + _, _, ok = cpp.ParseString(`"foo`) |
| 30 | + require.Equal(t, false, ok) |
| 31 | + |
| 32 | + str, rest, ok := cpp.ParseString(`"foo"`) |
| 33 | + require.Equal(t, true, ok) |
| 34 | + require.Equal(t, `foo`, str) |
| 35 | + require.Equal(t, ``, rest) |
| 36 | + |
| 37 | + str, rest, ok = cpp.ParseString(`"foo\\bar"`) |
| 38 | + require.Equal(t, true, ok) |
| 39 | + require.Equal(t, `foo\bar`, str) |
| 40 | + require.Equal(t, ``, rest) |
| 41 | + |
| 42 | + str, rest, ok = cpp.ParseString(`"foo \"is\" quoted and \\\\bar\"\" escaped\\" and "then" some`) |
| 43 | + require.Equal(t, true, ok) |
| 44 | + require.Equal(t, `foo "is" quoted and \\bar"" escaped\`, str) |
| 45 | + require.Equal(t, ` and "then" some`, rest) |
| 46 | + |
| 47 | + str, rest, ok = cpp.ParseString(`" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_abcdefghijklmnopqrstuvwxyz{|}~"`) |
| 48 | + require.Equal(t, true, ok) |
| 49 | + require.Equal(t, ` !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_abcdefghijklmnopqrstuvwxyz{|}~`, str) |
| 50 | + require.Equal(t, ``, rest) |
| 51 | + |
| 52 | + str, rest, ok = cpp.ParseString(`"/home/ççç/"`) |
| 53 | + require.Equal(t, true, ok) |
| 54 | + require.Equal(t, `/home/ççç/`, str) |
| 55 | + require.Equal(t, ``, rest) |
| 56 | + |
| 57 | + str, rest, ok = cpp.ParseString(`"/home/ççç/ /$sdsdd\\"`) |
| 58 | + require.Equal(t, true, ok) |
| 59 | + require.Equal(t, `/home/ççç/ /$sdsdd\`, str) |
| 60 | + require.Equal(t, ``, rest) |
| 61 | +} |
| 62 | + |
| 63 | +func TestQuoteString(t *testing.T) { |
| 64 | + cases := map[string]string{ |
| 65 | + `foo`: `"foo"`, |
| 66 | + `foo\bar`: `"foo\\bar"`, |
| 67 | + `foo "is" quoted and \\bar"" escaped\`: `"foo \"is\" quoted and \\\\bar\"\" escaped\\"`, |
| 68 | + // ASCII 0x20 - 0x7e, excluding ` |
| 69 | + ` !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_abcdefghijklmnopqrstuvwxyz{|}~`: `" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_abcdefghijklmnopqrstuvwxyz{|}~"`, |
| 70 | + } |
| 71 | + for input, expected := range cases { |
| 72 | + require.Equal(t, expected, cpp.QuoteString(input)) |
| 73 | + } |
| 74 | +} |
0 commit comments