github.com/TomSuzuki/white600@v1.0.2/white600_test.go (about)

     1  /*
     2   * white600_test
     3   * available at https://github.com/TomSuzuki/white600/
     4   *
     5   * Copyright 2022 TomSuzuki
     6   * LICENSE: MIT
     7   *
     8   * # How to use
     9   * > gotest -run NONE -bench .
    10   * > gotest -v
    11   */
    12  
    13  //package white600
    14  package white600_test
    15  
    16  import (
    17  	"html/template"
    18  	"io/ioutil"
    19  	"path/filepath"
    20  	"strings"
    21  	"testing"
    22  
    23  	"github.com/TomSuzuki/white600"
    24  )
    25  
    26  type testFile struct {
    27  	markdown string
    28  	html     string
    29  }
    30  
    31  func Test(t *testing.T) {
    32  	// test case (markdown, html)
    33  	dir := "./testcase/"
    34  	var testfile []testFile
    35  
    36  	// get list
    37  	files, _ := ioutil.ReadDir("./testcase/")
    38  	for _, file := range files {
    39  		if !file.IsDir() && filepath.Ext(file.Name()) == ".md" {
    40  			testfile = append(testfile, testFile{
    41  				markdown: dir + file.Name(),
    42  				html:     dir + file.Name() + ".html",
    43  			})
    44  		}
    45  	}
    46  
    47  	// test
    48  	for i := range testfile {
    49  		test(testfile[i], t)
    50  	}
    51  }
    52  
    53  // speed test
    54  func BenchmarkSpeed_white600(b *testing.B) {
    55  	file := "./testcase/00.md"
    56  	md, _ := ioutil.ReadFile(file)
    57  
    58  	b.ResetTimer()
    59  	for ct := 0; ct < 1500; ct++ {
    60  		white600.MarkdownToHTML(string(md))
    61  	}
    62  }
    63  
    64  func test(test testFile, t *testing.T) {
    65  	// load
    66  	b, _ := ioutil.ReadFile(test.html)
    67  	sample := string(b)
    68  	b, _ = ioutil.ReadFile(test.markdown)
    69  	answer := string(b)
    70  
    71  	// html -> markdown
    72  	answer = white600.MarkdownToHTML(answer)
    73  
    74  	// trim
    75  	sample = strings.NewReplacer("\r\n", "", "\r", "", "\n", "", " ", "", "'", "\"").Replace(sample)
    76  	answer = strings.NewReplacer("\r\n", "", "\r", "", "\n", "", " ", "", "'", "\"").Replace(answer)
    77  
    78  	// html
    79  	sampleHTML := template.HTML(sample)
    80  	answerHTML := template.HTML(answer)
    81  
    82  	// check
    83  	if sampleHTML != answerHTML {
    84  		t.Logf("☒  failed test: \t%s", test.markdown)
    85  		t.Logf(" - sample: %s", sampleHTML)
    86  		t.Logf(" - answer: %s", answerHTML)
    87  		t.Logf("")
    88  		t.Fail()
    89  	} else {
    90  		t.Logf("☑  success test: \t%s", test.markdown)
    91  	}
    92  }