-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
045d35a
commit 238a93e
Showing
6 changed files
with
119 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
name: Test | ||
|
||
on: | ||
- push | ||
- pull_request | ||
|
||
defaults: | ||
run: | ||
working-directory: ./HTMLConverter/ | ||
|
||
jobs: | ||
test: | ||
name: Test | ||
runs-on: macos-latest | ||
|
||
steps: | ||
- name: Check out Git repository | ||
uses: actions/checkout@v2 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v1 | ||
with: | ||
go-version: 1.14 | ||
|
||
- name: Run tests | ||
run: go test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/tdewolff/minify/v2" | ||
"github.com/tdewolff/minify/v2/html" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
var minifier *minify.M | ||
|
||
func minifyHTML(htmlString string) string { | ||
// Initialize minifier if necessary | ||
if minifier == nil { | ||
minifier = minify.New() | ||
minifier.Add("text/html", &html.Minifier{KeepEndTags: true, KeepQuotes: true}) | ||
} | ||
|
||
minified, err := minifier.String("text/html", htmlString) | ||
if err != nil { | ||
panic(fmt.Sprintf("Could not minify HTML: %d", err)) | ||
} | ||
return minified | ||
} | ||
|
||
func TestConvertCodeToHTML(t *testing.T) { | ||
source := `const print = (text) => console.log(text); | ||
print("Hello world");` | ||
actual := convertToGoString(convertCodeToHTML(convertToCString(source), convertToCString("js"))) | ||
actualTrimmed := strings.TrimSpace(actual) | ||
assert.True(t, strings.HasPrefix(actualTrimmed, `<pre class="chroma">`)) | ||
assert.True(t, strings.HasSuffix(actualTrimmed, `</pre>`)) | ||
} | ||
|
||
func TestConvertMarkdownToHTML(t *testing.T) { | ||
source := `# Heading | ||
Text` | ||
expected := "<h1>Heading</h1><p>Text</p>" | ||
actual := convertToGoString(convertMarkdownToHTML(convertToCString(source))) | ||
assert.Equal(t, expected, minifyHTML(actual)) | ||
} | ||
|
||
func TestConvertNotebookToHTML(t *testing.T) { | ||
source := `{"cells":[{"cell_type":"code","execution_count":1,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["Hello world\n"]}],"source":["print(\"Hello world\")"]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.8.2"}},"nbformat":4,"nbformat_minor":4}` | ||
actual := convertToGoString(convertNotebookToHTML(convertToCString(source))) | ||
actualTrimmed := strings.TrimSpace(actual) | ||
assert.True(t, strings.HasPrefix(actualTrimmed, `<div class="notebook">`)) | ||
assert.True(t, strings.HasSuffix(actualTrimmed, `</div>`)) | ||
} | ||
|
||
func TestConvertNotebookToHTMLInvalid(t *testing.T) { | ||
source := "This is not a valid JSON file." | ||
actual := convertToGoString(convertNotebookToHTML(convertToCString(source))) | ||
assert.True(t, strings.HasPrefix(actual, "error: ")) | ||
} |