github.com/jmigpin/editor@v1.6.0/util/parseutil/reslocparser/reslocparser_test.go (about)

     1  package reslocparser
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/jmigpin/editor/util/testutil"
     7  )
     8  
     9  func TestResLocParser1(t *testing.T) {
    10  	in := "AAA /a/b/c●.txt BBB"
    11  	out := "/a/b/c.txt"
    12  	testMode1(t, in, out)
    13  }
    14  func TestResLocParser2(t *testing.T) {
    15  	in := "AAA /a/b/●c%20c.txt AAA"
    16  	out := "/a/b/c c.txt"
    17  	testMode1(t, in, out)
    18  }
    19  func TestResLocParser3(t *testing.T) {
    20  	in := "AAA /a/b/●c\\ c.txt AAA"
    21  	out := "/a/b/c\\ c.txt"
    22  	testMode1(t, in, out)
    23  }
    24  func TestResLocParser4(t *testing.T) {
    25  	in := "AAA /a/b/●c\\\\ c.txt AAA"
    26  	out := "/a/b/c\\\\"
    27  	testMode1(t, in, out)
    28  }
    29  func TestResLocParser5(t *testing.T) {
    30  	in := "/a/b/c\\● c.txt"
    31  	out := "/a/b/c\\ c.txt"
    32  	testMode1(t, in, out)
    33  }
    34  func TestResLocParser5b(t *testing.T) {
    35  	in := "/a/b/c\\\\\\ \\ ●c.txt"
    36  	out := "/a/b/c\\\\\\ \\ c.txt"
    37  	testMode1(t, in, out)
    38  }
    39  func TestResLocParser5c(t *testing.T) {
    40  	in := "AAA /a/b/c\\ ●c.txt AAA"
    41  	out := "/a/b/c\\ c.txt"
    42  	testMode1(t, in, out)
    43  }
    44  func TestResLocParser5d(t *testing.T) {
    45  	in := "AAA /a/b/c\\ c●.txt AAA"
    46  	out := "/a/b/c\\ c.txt"
    47  	testMode1(t, in, out)
    48  }
    49  func TestResLocParser5e(t *testing.T) {
    50  	in := "●/a/b/c\\ c.txt AAA"
    51  	out := "/a/b/c\\ c.txt"
    52  	testMode1(t, in, out)
    53  }
    54  func TestResLocParser5f(t *testing.T) {
    55  	in := " a\\● b\\ c.txt"
    56  	out := "a\\ b\\ c.txt"
    57  	testMode1(t, in, out)
    58  }
    59  func TestResLocParser6(t *testing.T) {
    60  	in := "AAA /a/b/c.●txt\\:a:1:2# AAA"
    61  	out := "/a/b/c.txt\\:a:1:2"
    62  	testMode1(t, in, out)
    63  }
    64  func TestResLocParser7(t *testing.T) {
    65  	in := "AAA /a/b/c.●txt\\:a:1:#AAA"
    66  	out := "/a/b/c.txt\\:a:1"
    67  	testMode1(t, in, out)
    68  }
    69  func TestResLocParser8(t *testing.T) {
    70  	in := "●/a/b/c:1:2"
    71  	out := "/a/b/c:1:2"
    72  	testMode1(t, in, out)
    73  }
    74  func TestResLocParser9(t *testing.T) {
    75  	in := "●/a/b\\ b/c"
    76  	out := "/a/b\\ b/c"
    77  	testMode1(t, in, out)
    78  }
    79  func TestResLocParser10(t *testing.T) {
    80  	in := "●/a/b\\"
    81  	out := "/a/b"
    82  	testMode1(t, in, out)
    83  }
    84  func TestResLocParser11(t *testing.T) {
    85  	in := ": /a/b/c●"
    86  	out := "/a/b/c"
    87  	testMode1(t, in, out)
    88  }
    89  func TestResLocParser12(t *testing.T) {
    90  	in := "//a/b/////c●"
    91  	out := "/a/b/c"
    92  	testMode1(t, in, out)
    93  }
    94  func TestResLocParser13(t *testing.T) {
    95  	in := "(/a/b●/c.txt)"
    96  	out := "/a/b/c.txt"
    97  	testMode1(t, in, out)
    98  }
    99  func TestResLocParser14(t *testing.T) {
   100  	in := "[/a/b●/c.txt]"
   101  	out := "/a/b/c.txt"
   102  	testMode1(t, in, out)
   103  }
   104  func TestResLocParser15(t *testing.T) {
   105  	in := "</a/b●/c.txt>"
   106  	out := "/a/b/c.txt"
   107  	testMode1(t, in, out)
   108  }
   109  func TestResLocParser17(t *testing.T) {
   110  	in := "./a●/b/c.txt :20"
   111  	out := "./a/b/c.txt"
   112  	testMode1(t, in, out)
   113  }
   114  func TestResLocParser18(t *testing.T) {
   115  	in := "aa ●file:///a/b/c.txt bb"
   116  	out := "/a/b/c.txt"
   117  	testMode1(t, in, out)
   118  }
   119  func TestResLocParser18b(t *testing.T) {
   120  	in := "aa file://●/a/b/c.txt bb"
   121  	out := "/a/b/c.txt"
   122  	testMode1(t, in, out)
   123  }
   124  func TestResLocParser18c(t *testing.T) {
   125  	in := "aa file:///a/b/●c.txt bb"
   126  	out := "/a/b/c.txt"
   127  	testMode1(t, in, out)
   128  }
   129  func TestResLocParser19(t *testing.T) {
   130  	in := "aa &{file:///a/●b/c.txt}"
   131  	out := "/a/b/c.txt"
   132  	testMode1(t, in, out)
   133  }
   134  func TestResLocParser20(t *testing.T) {
   135  	in := "aa &{file:///a/●b/c%2b%2b.txt}"
   136  	out := "/a/b/c++.txt"
   137  	testMode1(t, in, out)
   138  }
   139  func TestResLocParser21(t *testing.T) {
   140  	in := "-arg=/a/●b/c.txt"
   141  	out := "/a/b/c.txt"
   142  	testMode1(t, in, out)
   143  }
   144  func TestResLocParser22(t *testing.T) {
   145  	in := "/a/b/●!u!w.txt"
   146  	out := "/a/b/!u!w.txt"
   147  	testMode1(t, in, out)
   148  }
   149  func TestResLocParser23(t *testing.T) {
   150  	in := "\"a/b/c.txt\", line 10●"
   151  	out := "a/b/c.txt:10"
   152  	testMode1(t, in, out)
   153  }
   154  func TestResLocParser24(t *testing.T) {
   155  	in := "bbb \"aa.py\", line● 10, in <bb>"
   156  	out := "aa.py:10"
   157  	testMode1(t, in, out)
   158  }
   159  func TestResLocParser25(t *testing.T) {
   160  	in := "bbb \"aa.py\", line 10●, in <bb>"
   161  	out := "aa.py:10"
   162  	testMode1(t, in, out)
   163  }
   164  func TestResLocParser26(t *testing.T) {
   165  	in := "bbb \"a●a.py\", line 10, in <bb>"
   166  	out := "aa.py:10"
   167  	testMode1(t, in, out)
   168  }
   169  func TestResLocParser27(t *testing.T) {
   170  	in := "bbb \"a●a.py\" bbb"
   171  	out := "aa.py"
   172  	testMode1(t, in, out)
   173  }
   174  func TestResLocParser28(t *testing.T) {
   175  	in := "/a/b.txt:●3"
   176  	out := "/a/b.txt:3"
   177  	testMode1(t, in, out)
   178  }
   179  func TestResLocParser29(t *testing.T) {
   180  	in := "file:/●//a/b.txt"
   181  	out := "/a/b.txt"
   182  	testMode1(t, in, out)
   183  }
   184  func TestResLocParser30(t *testing.T) {
   185  	in := "●file:///a/b.txt:1:1"
   186  	out := "/a/b.txt:1:1"
   187  	testMode1(t, in, out)
   188  }
   189  func TestResLocParser31(t *testing.T) {
   190  	in := "/a/b.t●xt: line 2: etc"
   191  	out := "/a/b.txt:2"
   192  	testMode1(t, in, out)
   193  }
   194  func TestResLocParser32(t *testing.T) {
   195  	in := "/a/b.txt: line ●2: etc"
   196  	out := "/a/b.txt:2"
   197  	testMode1(t, in, out)
   198  }
   199  func TestResLocParser33(t *testing.T) {
   200  	in := "\"/a/b.●txt\""
   201  	out := "/a/b.txt"
   202  	testMode1(t, in, out)
   203  }
   204  
   205  //----------
   206  
   207  func TestResLocParserWin1(t *testing.T) {
   208  	in := "++c:\\a\\b.t^ xt:3●"
   209  	out := "c:\\a\\b.t^ xt:3"
   210  	testMode2(t, in, out, '^', '\\', true)
   211  }
   212  func TestResLocParserWin2(t *testing.T) {
   213  	in := "file:///c:/a/b.txt:3●"
   214  	out := "c:\\a\\b.txt:3"
   215  	testMode2(t, in, out, '^', '\\', true)
   216  }
   217  func TestResLocParserWin3(t *testing.T) {
   218  	in := "..\\\nabc\\●"
   219  	out := "abc\\"
   220  	testMode2(t, in, out, '^', '\\', true)
   221  }
   222  
   223  //----------
   224  //----------
   225  //----------
   226  
   227  func BenchmarkResLoc1(b *testing.B) {
   228  	t := b
   229  	in := "/a/b/c.●txt:1:2"
   230  	in2, index, err := testutil.SourceCursor("●", string(in), 0)
   231  	if err != nil {
   232  		t.Fatal(err)
   233  	}
   234  
   235  	p := NewResLocParser()
   236  	p.Init()
   237  
   238  	b.ResetTimer()
   239  	for i := 0; i < b.N; i++ {
   240  		rl, err := p.Parse([]byte(in2), index)
   241  		if err != nil {
   242  			t.Fatal(err)
   243  		}
   244  		_ = rl
   245  	}
   246  }
   247  
   248  //----------
   249  //----------
   250  //----------
   251  
   252  func testMode1(t *testing.T, in, out string) {
   253  	t.Helper()
   254  	testMode2(t, in, out, 0, 0, false)
   255  }
   256  func testMode2(t *testing.T, in, out string, esc, psep rune, parseVolume bool) {
   257  	t.Helper()
   258  
   259  	//in = string(bytes.TrimRight(in, "\n"))
   260  	//out = string(bytes.TrimRight(out, "\n"))
   261  
   262  	in2, index, err := testutil.SourceCursor("●", string(in), 0)
   263  	if err != nil {
   264  		t.Fatal(err)
   265  	}
   266  
   267  	p := NewResLocParser()
   268  
   269  	// setup options
   270  	if esc != 0 {
   271  		p.Escape = esc
   272  	}
   273  	if psep != 0 {
   274  		p.PathSeparator = psep
   275  	}
   276  	p.ParseVolume = parseVolume
   277  
   278  	p.Init()
   279  
   280  	rl, err := p.Parse([]byte(in2), index)
   281  	if err != nil {
   282  		t.Fatal(err)
   283  	}
   284  	res := rl.Stringify1()
   285  
   286  	res2 := testutil.TrimLineSpaces(res)
   287  	expect2 := testutil.TrimLineSpaces(out)
   288  	if res2 != expect2 {
   289  		//t.Fatalf("res=%v\n%v\n", res, rl.Bnd.SprintRuleTree(5))
   290  		t.Fatalf("res=%v", res)
   291  	}
   292  }