aboutsummaryrefslogtreecommitdiffhomepage
path: root/backend/game/hub_test.go
blob: a8fad58fce2a2bea41d6253d52f8f3d59a096c8d (plain)
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
package game

import "testing"

func TestCalcCodeSize_PHP(t *testing.T) {
	hub := &Hub{}
	tests := []struct {
		name     string
		code     string
		language string
		want     int
	}{
		{
			name:     "simple php code",
			code:     "<?php echo 1;",
			language: "php",
			want:     6, // "echo1;" after stripping whitespace and "<?php"
		},
		{
			name:     "php with short open tag",
			code:     "<? echo 1;",
			language: "php",
			want:     6, // "echo1;" after stripping whitespace and "<?"
		},
		{
			name:     "php with closing tag",
			code:     "<?php echo 1; ?>",
			language: "php",
			want:     6, // "echo1;" after stripping whitespace, "<?php", and "?>"
		},
		{
			name:     "php with whitespace",
			code:     "<?php echo   1 ;  ?>",
			language: "php",
			want:     6,
		},
		{
			name:     "non-php language",
			code:     "print(1)",
			language: "swift",
			want:     8,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := hub.CalcCodeSize(tt.code, tt.language)
			if got != tt.want {
				t.Errorf("CalcCodeSize(%q, %q) = %d, want %d", tt.code, tt.language, got, tt.want)
			}
		})
	}
}

func TestIsTestcaseResultCorrect(t *testing.T) {
	tests := []struct {
		name     string
		expected string
		actual   string
		want     bool
	}{
		{
			name:     "exact match",
			expected: "hello",
			actual:   "hello",
			want:     true,
		},
		{
			name:     "trailing newline ignored",
			expected: "hello\n",
			actual:   "hello",
			want:     true,
		},
		{
			name:     "CRLF normalized",
			expected: "hello\r\n",
			actual:   "hello\n",
			want:     true,
		},
		{
			name:     "mismatch",
			expected: "hello",
			actual:   "world",
			want:     false,
		},
		{
			name:     "multiline match",
			expected: "line1\nline2",
			actual:   "line1\nline2\n",
			want:     true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := isTestcaseResultCorrect(tt.expected, tt.actual)
			if got != tt.want {
				t.Errorf("isTestcaseResultCorrect(%q, %q) = %v, want %v", tt.expected, tt.actual, got, tt.want)
			}
		})
	}
}