File: parser_test.go

package info (click to toggle)
golang-github-huandu-go-assert 1.1.5-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 136 kB
  • sloc: makefile: 2
file content (119 lines) | stat: -rw-r--r-- 2,041 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2017 Huan Du. All rights reserved.
// Licensed under the MIT license that can be found in the LICENSE file.

package assertion

import (
	"testing"
)

func assertEqual(t *testing.T, v1, v2 interface{}) {
	AssertEqual(t, v1, v2, &Trigger{
		FuncName: "assertEqual",
		Skip:     1,
		Args:     []int{1, 2},
	})
}

func TestParseFalseKind(t *testing.T) {
	cases := []struct {
		Value interface{}
		Kind  FalseKind
	}{
		{
			12, Positive,
		},
		{
			nil, Nil,
		},
		{
			0, Zero,
		},
		{
			uint(0), Zero,
		},
		{
			0.0, Zero,
		},
		{
			complex(0, 0), Zero,
		},
		{
			false, False,
		},
		{
			[]int{}, Positive,
		},
		{
			([]int)(nil), Nil,
		},
		{
			"", EmptyString,
		},
	}

	for i, c := range cases {
		t.Logf("case %v: %v", i, c)
		k := ParseFalseKind(c.Value)
		assertEqual(t, c.Kind, k)
	}
}

func TestParseArgs(t *testing.T) {
	cases := []struct {
		ArgIndex    []int
		Args        []string
		Assignments [][]string
		RelatedVars []string
	}{
		{
			[]int{0},
			[]string{`prefix + args`},
			[][]string{
				{`f(&args)`, `prefix := s.(type)`},
			},
			[]string{`args`, `prefix`, `s`},
		},
		{
			[]int{1},
			[]string{`skip`},
			[][]string{
				{`skip = 0`},
			},
			[]string{},
		},
		{
			[]int{-1, 0, -2, 4},
			[]string{`c.ArgIndex`, `prefix + args`, `skip`, ""},
			[][]string{
				{`i, c := range cases`},
				{`f(&args)`, `prefix := s.(type)`},
				{`skip = 0`},
				nil,
			},
			[]string{`args`, `c`, `i`, `prefix`, `s`},
		},
	}
	p := new(Parser)

	for i, c := range cases {
		skip := i
		skip = 0 // The last assignment to `skip` should be chosen.
		args := "foo"
		f := func(s *string) { *s = "Args" }
		f(&args)

		var s interface{} = "Parse"
		switch prefix := s.(type) { // Test init stmt in SwitchStmt.
		case string:
			f, err := p.ParseArgs(prefix+args, skip, c.ArgIndex)
			info := p.ParseInfo(f)
			skip = 2

			assertEqual(t, err, nil)
			assertEqual(t, info.Args, c.Args)
			assertEqual(t, info.Assignments, c.Assignments)
			assertEqual(t, info.RelatedVars, c.RelatedVars)
		}
	}
}