Skip to content

Commit f50eb59

Browse files
committed
- [+] add & use prop_test.go for Piece Attributes testing
1 parent 4bf3f72 commit f50eb59

File tree

7 files changed

+111
-95
lines changed

7 files changed

+111
-95
lines changed

cascadia_test.go

Lines changed: 0 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -549,98 +549,3 @@ var selectorTests = []selectorTest{
549549
},
550550
},
551551
}
552-
553-
type PieceAttrTest struct {
554-
HTML, selector string
555-
results []string
556-
piece OutputStyleMap
557-
}
558-
559-
var PieceAttrTests = []PieceAttrTest{
560-
{
561-
`<ul>
562-
<li><a id="a1" href="http://www.google.com/finance"/>
563-
<li><a id="a2" href="http://finance.yahoo.com/"/>
564-
<li><a id="a3" href="https://www.google.com/news"></a>
565-
<li><a id="a4" href="http://news.yahoo.com"/>
566-
</ul>`,
567-
`li`,
568-
[]string{
569-
`id,`,
570-
`a1,`,
571-
`a2,`,
572-
`a3,`,
573-
`a4,`,
574-
},
575-
OutputStyleMap{
576-
[]string{"id"},
577-
map[string]string{"id": "a"},
578-
map[string]OutputStyle{"id": OutputStyleATTR},
579-
map[string]string{"id": "id"},
580-
},
581-
},
582-
{
583-
`<ul>
584-
<li><a id="a1" href="http://www.google.com/finance"/>
585-
<li><a id="a2" href="http://finance.yahoo.com/"/>
586-
<li><a id="a3" href="https://www.google.com/news"></a>
587-
<li><a id="a4" href="http://news.yahoo.com"/>
588-
</ul>`,
589-
`li`,
590-
[]string{
591-
`href2,`,
592-
`,`,
593-
`,`,
594-
`,`,
595-
`,`,
596-
},
597-
OutputStyleMap{
598-
[]string{"href2"},
599-
map[string]string{"href2": "a"},
600-
map[string]OutputStyle{"href2": OutputStyleATTR},
601-
map[string]string{"href2": "href2"},
602-
},
603-
},
604-
{
605-
`<ul>
606-
<li><a id="a1" href="http://www.google.com/finance"/>
607-
<li><a id="a2" href="http://finance.yahoo.com/"/>
608-
<li><a id="a3" href="https://www.google.com/news"></a>
609-
<li><a id="a4" href="http://news.yahoo.com"/>
610-
</ul>`,
611-
`li`,
612-
[]string{
613-
`href,`,
614-
`http://www.google.com/finance,`,
615-
`http://finance.yahoo.com/,`,
616-
`https://www.google.com/news,`,
617-
`http://news.yahoo.com,`,
618-
},
619-
OutputStyleMap{
620-
[]string{"href"},
621-
map[string]string{"href": "a"},
622-
map[string]OutputStyle{"href": OutputStyleATTR},
623-
map[string]string{"href": "href"},
624-
},
625-
},
626-
}
627-
628-
func TestPieceAttr(t *testing.T) {
629-
for _, test := range PieceAttrTests {
630-
buf := bytes.NewBufferString("")
631-
Opts.CSS, Opts.Piece, Opts.Deli,
632-
Opts.WrapHTML, Opts.TextOut, Opts.TextRaw, Opts.Quiet =
633-
[]string{test.selector}, test.piece, ",",
634-
false, false, false, false
635-
Cascadia(strings.NewReader(test.HTML), buf, Opts)
636-
got := buf.String()
637-
if len(got) == 0 && len(test.results) == 0 {
638-
// correct
639-
continue
640-
}
641-
want := strings.Join(test.results, "\n") + "\n"
642-
if got != want {
643-
t.Errorf("wanted %s, got %s instead", want, got)
644-
}
645-
}
646-
}

piece_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main_test
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestPieceAttr(t *testing.T) {
8+
testCases(t, "Piece Attributes", []testCase{
9+
{"id",
10+
[]string{"-i", "opt_piece_attr.html", "-o", "-c", "li", "-p", "url=attr[id]:a"},
11+
},
12+
{"href",
13+
[]string{"-i", "opt_piece_attr.html", "-o", "-c", "li", "-p", "url=attr[href]:a"},
14+
},
15+
//{"both_fields",},
16+
//{"noexist"},
17+
//{},
18+
})
19+
}

prop_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package main_test
2+
3+
import (
4+
"bytes"
5+
"os"
6+
"os/exec"
7+
"strings"
8+
"testing"
9+
)
10+
11+
const (
12+
dirTest = "test/"
13+
cmdTest = "../cascadia"
14+
extRef = ".ref" // extension for reference file
15+
extGot = ".got" // extension for generated file
16+
)
17+
18+
// testIt runs @cmdTest with @argv and compares the generated
19+
// output for @name with the corresponding @extRef
20+
func testIt(t *testing.T, name string, argv ...string) {
21+
var (
22+
diffOut bytes.Buffer
23+
generatedOutput = name + extGot
24+
cmd = exec.Command(cmdTest, argv...)
25+
)
26+
27+
t.Logf("Testing %s:\n\t%s %s\n", name, cmdTest, strings.Join(argv, " "))
28+
29+
// open the out file for writing
30+
outfile, err := os.Create(generatedOutput)
31+
if err != nil {
32+
t.Errorf("write error [%s: %s] %s.", name, argv, err)
33+
}
34+
defer outfile.Close()
35+
cmd.Stdout = outfile
36+
37+
err = cmd.Start()
38+
if err != nil {
39+
t.Errorf("start error [%s: %s] %s.", name, argv, err)
40+
}
41+
err = cmd.Wait()
42+
if err != nil {
43+
t.Errorf("exit error [%s: %s] %s.", name, argv, err)
44+
}
45+
46+
cmd = exec.Command("diff", "-U1", name+extRef, generatedOutput)
47+
cmd.Stdout = &diffOut
48+
49+
err = cmd.Start()
50+
if err != nil {
51+
t.Errorf("start error %s [%s: %s]", err, name, argv)
52+
}
53+
err = cmd.Wait()
54+
if err != nil {
55+
t.Errorf("cmp error %s [%s: %s]\n%s", err, name, argv, diffOut.String())
56+
}
57+
//os.Remove(generatedOutput)
58+
}
59+
60+
type testCase struct {
61+
name string
62+
args []string
63+
}
64+
65+
func testCases(t *testing.T, name string, testData []testCase) {
66+
t.Logf("\n\n== Testing %s\n\n", name)
67+
os.Chdir(dirTest)
68+
69+
for _, tc := range testData {
70+
testIt(t, tc.name, tc.args...)
71+
}
72+
73+
}

test/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.got

test/href.ref

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
url
2+
http://www.google.com/finance
3+
http://finance.yahoo.com/
4+
https://www.google.com/news
5+
http://news.yahoo.com
6+

test/id.ref

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
url
2+
a1
3+
a2
4+
a3
5+
a4
6+

test/opt_piece_attr.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<ul>
2+
<li><a id="a1" href="http://www.google.com/finance"/>
3+
<li><a id="a2" href="http://finance.yahoo.com/"/>
4+
<li><a id="a3" href="https://www.google.com/news"></a>
5+
<li><a id="a4" href="http://news.yahoo.com"/>
6+
</ul>

0 commit comments

Comments
 (0)