aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTong Sun <[email protected]>2023-05-02 12:41:20 -0400
committerTong Sun <[email protected]>2023-05-02 12:43:18 -0400
commitf8e39207b0f4b56d29fa4b0711249462dd84b353 (patch)
tree51eba5d82a0070ca42f30a105337be5765972287
parent- [+] add prop_test.go from jsonfiddle (diff)
downloadhtml2md-f8e39207b0f4b56d29fa4b0711249462dd84b353.tar.xz
html2md-f8e39207b0f4b56d29fa4b0711249462dd84b353.zip
- [#] use real test to replace dummy test
-rw-r--r--.github/workflows/go-release-build.yml1
-rw-r--r--html2md_test.go59
-rw-r--r--main_test.go9
-rw-r--r--prop_test.go38
4 files changed, 70 insertions, 37 deletions
diff --git a/.github/workflows/go-release-build.yml b/.github/workflows/go-release-build.yml
index 8567413..13393bd 100644
--- a/.github/workflows/go-release-build.yml
+++ b/.github/workflows/go-release-build.yml
@@ -41,6 +41,7 @@ jobs:
ln -vs $GOPATH/src/github.com/$GITHUB_REPOSITORY $GITHUB_WORKSPACE
# go mod tidy
go get -v ./...
+ go build -v
go test -v ./...
-
diff --git a/html2md_test.go b/html2md_test.go
new file mode 100644
index 0000000..a9ed5ae
--- /dev/null
+++ b/html2md_test.go
@@ -0,0 +1,59 @@
+package main_test
+
+import (
+ "os"
+ "strings"
+ "testing"
+)
+
+const (
+ cmdTest = "./html2md"
+ dirTest = "."
+
+ boldText = "<strong>Bold Text</strong>"
+ boldEscape = "<strong>option src_ip</strong>"
+)
+
+type testCase struct {
+ name, out, in string
+ args []string
+}
+
+func TestExec(t *testing.T) {
+ testData := []testCase{
+ {
+ "BoldText", "**Bold Text**", boldText, []string{"-i"},
+ },
+ {
+ "BoldText-delimiter", "__Bold Text__", boldText,
+ []string{"-i", "--opt-strong-delimiter", "__"},
+ },
+ {
+ "BoldEscape", "**option src\\_ip**", boldEscape, []string{"-i"},
+ },
+ {
+ "Checkbox", "- [x] Checked!\n- [ ] Check Me!",
+ "<ul><li><input type=checkbox checked>Checked!</li><li><input type=checkbox>Check Me!</li></ul>",
+ []string{"-i", "-G"},
+ },
+ {
+ "PluginStrikethrough", "Only ~~blue ones~~ ~~left~~",
+ "Only <del>blue ones</del> <s> left</s>",
+ []string{"-i", "--plugin-strikethrough"},
+ },
+ // {
+ // "", "", "", []string{"-i"},
+ // },
+ }
+
+ os.Chdir(dirTest)
+
+ t.Logf("\n\n== Testing options and plugins\n\n")
+
+ for _, tc := range testData {
+ r := strings.TrimSpace(testIt(t, tc.name, tc.in, tc.args...))
+ if tc.out != r {
+ t.Errorf(`expected "%s" but got "%s" instead`, tc.out, r)
+ }
+ }
+}
diff --git a/main_test.go b/main_test.go
deleted file mode 100644
index 1504011..0000000
--- a/main_test.go
+++ /dev/null
@@ -1,9 +0,0 @@
-package main_test
-
-import (
- "testing"
-)
-
-func TestNothing(t *testing.T) {
- t.Logf("Dummy test passed")
-}
diff --git a/prop_test.go b/prop_test.go
index 6e73a53..2d3d4e2 100644
--- a/prop_test.go
+++ b/prop_test.go
@@ -2,32 +2,25 @@ package main_test
import (
"bytes"
- "os"
"os/exec"
"strings"
"testing"
)
-// testIt runs @cmdTest with @argv and compares the generated
-// output for @name with the corresponding @extRef reference file
-func testIt(t *testing.T, name string, argv ...string) {
+// testIt runs @cmdTest with @argv and @in as stdin in @dirTest
+// then return the output
+func testIt(t *testing.T, name, in string, argv ...string) string {
var (
- diffOut bytes.Buffer
- generatedOutput = name + extGot
- cmd = exec.Command(cmdTest, argv...)
+ appOut bytes.Buffer
+ cmd = exec.Command(cmdTest, argv...)
)
- t.Logf("Testing %s:\n\t%s %s\n", name, cmdTest, strings.Join(argv, " "))
+ t.Logf("Testing '%s':\n\t%s %s\n", name, cmdTest, strings.Join(argv, " "))
- // open the out file for writing
- outfile, err := os.Create(generatedOutput)
- if err != nil {
- t.Errorf("write error [%s: %s] %s.", name, argv, err)
- }
- defer outfile.Close()
- cmd.Stdout = outfile
+ cmd.Stdin = strings.NewReader(in)
+ cmd.Stdout = &appOut
- err = cmd.Start()
+ err := cmd.Start()
if err != nil {
t.Errorf("start error [%s: %s] %s.", name, argv, err)
}
@@ -36,16 +29,5 @@ func testIt(t *testing.T, name string, argv ...string) {
t.Errorf("exit error [%s: %s] %s.", name, argv, err)
}
- cmd = exec.Command("diff", "-U1", name+extRef, generatedOutput)
- cmd.Stdout = &diffOut
-
- err = cmd.Start()
- if err != nil {
- t.Errorf("start error %s [%s: %s]", err, name, argv)
- }
- err = cmd.Wait()
- if err != nil {
- t.Errorf("cmp error %s [%s: %s]\n%s", err, name, argv, diffOut.String())
- }
- //os.Remove(generatedOutput)
+ return appOut.String()
}