github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/yaml/printer/printer_test.go (about)

     1  package printer_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/bingoohuang/gg/pkg/yaml/lexer"
     8  	"github.com/bingoohuang/gg/pkg/yaml/printer"
     9  )
    10  
    11  func Test_Printer(t *testing.T) {
    12  	yml := `---
    13  text: aaaa
    14  text2: aaaa
    15   bbbb
    16   cccc
    17   dddd
    18   eeee
    19  text3: ffff
    20   gggg
    21   hhhh
    22   iiii
    23   jjjj
    24  bool: true
    25  number: 10
    26  anchor: &x 1
    27  alias: *x
    28  `
    29  	t.Run("print starting from tokens[3]", func(t *testing.T) {
    30  		tokens := lexer.Tokenize(yml)
    31  		var p printer.Printer
    32  		actual := "\n" + p.PrintErrorToken(tokens[3], false)
    33  		expect := `
    34     1 | ---
    35  >  2 | text: aaaa
    36               ^
    37     3 | text2: aaaa
    38     4 |  bbbb
    39     5 |  cccc
    40     6 |  dddd
    41     7 |  eeee
    42     8 | `
    43  		if actual != expect {
    44  			t.Fatalf("unexpected output: expect:[%s]\n actual:[%s]", expect, actual)
    45  		}
    46  	})
    47  	t.Run("print starting from tokens[4]", func(t *testing.T) {
    48  		tokens := lexer.Tokenize(yml)
    49  		var p printer.Printer
    50  		actual := "\n" + p.PrintErrorToken(tokens[4], false)
    51  		expect := `
    52     1 | ---
    53     2 | text: aaaa
    54  >  3 | text2: aaaa
    55     4 |  bbbb
    56     5 |  cccc
    57     6 |  dddd
    58     7 |  eeee
    59         ^
    60  `
    61  		if actual != expect {
    62  			t.Fatalf("unexpected output: expect:[%s]\n actual:[%s]", expect, actual)
    63  		}
    64  	})
    65  	t.Run("print starting from tokens[6]", func(t *testing.T) {
    66  		tokens := lexer.Tokenize(yml)
    67  		var p printer.Printer
    68  		actual := "\n" + p.PrintErrorToken(tokens[6], false)
    69  		expect := `
    70     1 | ---
    71     2 | text: aaaa
    72  >  3 | text2: aaaa
    73     4 |  bbbb
    74     5 |  cccc
    75     6 |  dddd
    76     7 |  eeee
    77                ^
    78     8 | text3: ffff
    79     9 |  gggg
    80    10 |  hhhh
    81    11 |  iiii
    82    12 |  jjjj
    83    13 | `
    84  		if actual != expect {
    85  			t.Fatalf("unexpected output: expect:[%s]\n actual:[%s]", expect, actual)
    86  		}
    87  	})
    88  	t.Run("print error token with document header", func(t *testing.T) {
    89  		tokens := lexer.Tokenize(`---
    90  a:
    91   b:
    92    c:
    93     d: e
    94     f: g
    95     h: i
    96  
    97  ---
    98  `)
    99  		expect := `
   100     3 |  b:
   101     4 |   c:
   102     5 |    d: e
   103  >  6 |    f: g
   104               ^
   105     7 |    h: i
   106     8 | 
   107     9 | ---`
   108  		var p printer.Printer
   109  		actual := "\n" + p.PrintErrorToken(tokens[12], false)
   110  		if actual != expect {
   111  			t.Fatalf("unexpected output: expect:[%s]\n actual:[%s]", expect, actual)
   112  		}
   113  	})
   114  	t.Run("output with color", func(t *testing.T) {
   115  		t.Run("token6", func(t *testing.T) {
   116  			tokens := lexer.Tokenize(yml)
   117  			var p printer.Printer
   118  			t.Logf("\n%s", p.PrintErrorToken(tokens[6], true))
   119  		})
   120  		t.Run("token9", func(t *testing.T) {
   121  			tokens := lexer.Tokenize(yml)
   122  			var p printer.Printer
   123  			t.Logf("\n%s", p.PrintErrorToken(tokens[9], true))
   124  		})
   125  		t.Run("token12", func(t *testing.T) {
   126  			tokens := lexer.Tokenize(yml)
   127  			var p printer.Printer
   128  			t.Logf("\n%s", p.PrintErrorToken(tokens[12], true))
   129  		})
   130  	})
   131  	t.Run("print error message", func(t *testing.T) {
   132  		var p printer.Printer
   133  		src := "message"
   134  		msg := p.PrintErrorMessage(src, false)
   135  		if msg != src {
   136  			t.Fatal("unexpected result")
   137  		}
   138  		p.PrintErrorMessage(src, true)
   139  	})
   140  }
   141  
   142  func TestPrinter_Anchor(t *testing.T) {
   143  	expected := `
   144  anchor: &x 1
   145  alias: *x`
   146  	tokens := lexer.Tokenize(expected)
   147  	var p printer.Printer
   148  	got := p.PrintTokens(tokens)
   149  	if expected != got {
   150  		t.Fatalf("unexpected output: expect:[%s]\n actual:[%s]", expected, got)
   151  	}
   152  }
   153  
   154  func Test_Printer_Multiline(t *testing.T) {
   155  	yml := `
   156  text1: 'aaaa
   157   bbbb
   158   cccc'
   159  text2: "ffff
   160   gggg
   161   hhhh"
   162  text3: hello
   163  `
   164  	tc := []struct {
   165  		token int
   166  		want  string
   167  	}{
   168  		{
   169  			token: 2,
   170  			want: `
   171  >  2 | text1: 'aaaa
   172     3 |  bbbb
   173     4 |  cccc'
   174                ^
   175     5 | text2: "ffff
   176     6 |  gggg
   177     7 |  hhhh"`,
   178  		},
   179  		{
   180  			token: 3,
   181  			want: `
   182     2 | text1: 'aaaa
   183     3 |  bbbb
   184     4 |  cccc'
   185  >  5 | text2: "ffff
   186     6 |  gggg
   187     7 |  hhhh"
   188         ^
   189     8 | text3: hello`,
   190  		},
   191  		{
   192  			token: 5,
   193  			want: `
   194     2 | text1: 'aaaa
   195     3 |  bbbb
   196     4 |  cccc'
   197  >  5 | text2: "ffff
   198     6 |  gggg
   199     7 |  hhhh"
   200                ^
   201     8 | text3: hello`,
   202  		},
   203  		{
   204  			token: 6,
   205  			want: `
   206     5 | text2: "ffff
   207     6 |  gggg
   208     7 |  hhhh"
   209  >  8 | text3: hello
   210         ^
   211  `,
   212  		},
   213  	}
   214  	for _, tt := range tc {
   215  		name := fmt.Sprintf("print starting from tokens[%d]", tt.token)
   216  		t.Run(name, func(t *testing.T) {
   217  			tokens := lexer.Tokenize(yml)
   218  			var p printer.Printer
   219  			got := "\n" + p.PrintErrorToken(tokens[tt.token], false)
   220  			want := tt.want
   221  			if got != want {
   222  				t.Fatalf("PrintErrorToken() got: %s\n want:%s\n", want, got)
   223  			}
   224  		})
   225  	}
   226  }