Skip to content

Commit

Permalink
use table test to reduce overall scan test complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
NarcisDavins committed May 31, 2018
1 parent e4874d7 commit 03ba632
Showing 1 changed file with 43 additions and 107 deletions.
150 changes: 43 additions & 107 deletions generator/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,121 +2,57 @@ package generator

import (
"fmt"
"log"
"os"
"testing"
)

func TestTmplScanner(t *testing.T) {
pwd := os.Getenv("PWD")
sourceFolder := fmt.Sprintf("%s/test/sources", pwd)

iso := "en-US"
paths := []string{
fmt.Sprintf("%s/global", sourceFolder),
fmt.Sprintf("%s/%s", sourceFolder, iso),
}
scanner := NewScanner(paths)

tmpls := scanner.Scan()
log.Println(tmpls)
if len(tmpls) != 2 {
t.Errorf("[%s] unexpected scan result. have %d nodes, want %d", iso, len(tmpls), 2)
return
}
if tmpls[0].Path != paths[0] {
t.Errorf("[%s - 0] unexpected path. have %s, want %s", iso, tmpls[0].Path, paths[0])
return
}
if len(tmpls[0].Content) != 4 {
t.Errorf("[%s - 0] unexpected content size. have %d, want 4", iso, len(tmpls[0].Content))
return
}
if tmpls[1].Path != paths[1] {
t.Errorf("[%s - 1] unexpected path. have %s, want %s", iso, tmpls[1].Path, paths[1])
return
}
if len(tmpls[1].Content) != 1 {
t.Errorf("[%s - 1] unexpected content size. have %d, want 1", iso, len(tmpls[1].Content))
return
tt := []struct {
iso string
nodes int
}{
{"en-US", 2},
{"en-UK", 2},
{"es-ES", 2},
{"unknown", 1},
}

iso = "en-UK"
paths = []string{
fmt.Sprintf("%s/global", sourceFolder),
fmt.Sprintf("%s/%s", sourceFolder, iso),
}
scanner = NewScanner(paths)

tmpls = scanner.Scan()
if len(tmpls) != 2 {
t.Errorf("[%s] unexpected scan result. have %d nodes, want %d", iso, len(tmpls), 2)
return
}
if tmpls[0].Path != paths[0] {
t.Errorf("[%s - 0] unexpected path. have %s, want %s", iso, tmpls[0].Path, paths[0])
return
}
if len(tmpls[0].Content) != 4 {
t.Errorf("[%s - 0] unexpected content size. have %d, want 4", iso, len(tmpls[0].Content))
return
}
if tmpls[1].Path != paths[1] {
t.Errorf("[%s - 1] unexpected path. have %s, want %s", iso, tmpls[1].Path, paths[1])
return
}
if len(tmpls[1].Content) != 1 {
t.Errorf("[%s - 1] unexpected content size. have %d, want 1", iso, len(tmpls[1].Content))
return
}

iso = "es-ES"
paths = []string{
fmt.Sprintf("%s/global", sourceFolder),
fmt.Sprintf("%s/%s", sourceFolder, iso),
}
scanner = NewScanner(paths)

tmpls = scanner.Scan()
if len(tmpls) != 2 {
t.Errorf("[%s] unexpected scan result. have %d nodes, want %d", iso, len(tmpls), 2)
return
}
if tmpls[0].Path != paths[0] {
t.Errorf("[%s - 0] unexpected path. have %s, want %s", iso, tmpls[0].Path, paths[0])
return
}
if len(tmpls[0].Content) != 4 {
t.Errorf("[%s - 0] unexpected content size. have %d, want 4", iso, len(tmpls[0].Content))
return
}
if tmpls[1].Path != paths[1] {
t.Errorf("[%s - 1] unexpected path. have %s, want %s", iso, tmpls[1].Path, paths[1])
return
}
if len(tmpls[1].Content) != 1 {
t.Errorf("[%s - 1] unexpected content size. have %d, want 1", iso, len(tmpls[1].Content))
return
}

iso = "unknown"
paths = []string{
fmt.Sprintf("%s/global", sourceFolder),
fmt.Sprintf("%s/%s", sourceFolder, iso),
}
scanner = NewScanner(paths)
pwd := os.Getenv("PWD")
sourceFolder := fmt.Sprintf("%s/test/sources", pwd)

tmpls = scanner.Scan()
if len(tmpls) != 1 {
t.Errorf("[%s] unexpected scan result. have %d nodes, want %d", iso, len(tmpls), 2)
return
}
if tmpls[0].Path != paths[0] {
t.Errorf("[%s - 0] unexpected path. have %s, want %s", iso, tmpls[0].Path, paths[0])
return
}
if len(tmpls[0].Content) != 4 {
t.Errorf("[%s - 0] unexpected content size. have %d, want 4", iso, len(tmpls[0].Content))
return
for _, tc := range tt {
t.Run(tc.iso, func(t *testing.T) {
paths := []string{
fmt.Sprintf("%s/global", sourceFolder),
fmt.Sprintf("%s/%s", sourceFolder, tc.iso),
}
scanner := NewScanner(paths)

tmpls := scanner.Scan()
if len(tmpls) != tc.nodes {
t.Errorf("[%s] unexpected scan result. have %d nodes, want %d", tc.iso, len(tmpls), tc.nodes)
return
}

if tmpls[0].Path != paths[0] {
t.Errorf("[%s - 0] unexpected path. have %s, want %s", tc.iso, tmpls[0].Path, paths[0])
return
}
if len(tmpls[0].Content) != 4 {
t.Errorf("[%s - 0] unexpected content size. have %d, want 4", tc.iso, len(tmpls[0].Content))
return
}
if tc.nodes > 1 {
if tmpls[1].Path != paths[1] {
t.Errorf("[%s - 1] unexpected path. have %s, want %s", tc.iso, tmpls[1].Path, paths[1])
return
}
if len(tmpls[1].Content) != 1 {
t.Errorf("[%s - 1] unexpected content size. have %d, want 1", tc.iso, len(tmpls[1].Content))
return
}
}
})
}
}

0 comments on commit 03ba632

Please sign in to comment.