bitbucket.org/bjjb/blogo@v0.0.0-20170919123308-06e1b3cd90aa/main_test.go (about)

     1  package main
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"runtime"
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  func TestBlogo(t *testing.T) {
    13  	tmpdir, _ := ioutil.TempDir("", "blogo_test")
    14  
    15  	_ = os.Chdir(tmpdir)
    16  
    17  	time, _ := time.Parse("2006-01-02 UTC", "2014-01-01 UTC")
    18  	_ = ioutil.WriteFile("test.md", []byte("*A* _b_"), 0777)
    19  	_ = os.Chtimes("test.md", time, time)
    20  
    21  	if err := rebuild(); err != nil {
    22  		t.Errorf("failed to rebuild: %e", err)
    23  	}
    24  
    25  	data, err := ioutil.ReadFile("test.html")
    26  
    27  	if err != nil {
    28  		t.Errorf("test.html not readable: %e", err)
    29  	}
    30  
    31  	content := string(data)
    32  
    33  	expected := `<!doctype html>
    34  <html>
    35    <head>
    36        <meta charset="utf8">
    37        <title>Test ∴ My Blog</title>
    38      <style>
    39        body { font-size; 1.5em; padding: 2em; }
    40        h1 { line-height: 2em; border-bottom: 1px solid black; }
    41      </style>
    42      <script>
    43        console.log("Hello, world!");
    44      </script>
    45    </head>
    46    <body>
    47      <article>
    48        <header>
    49          <h1>Test</h1>
    50          <time datetime="2014-01-01T00:00:00Z">1 Jan 2014</time>
    51        </header>
    52        <p><em>A</em> <em>b</em></p>
    53  
    54      </article>
    55    </body>
    56  </html>`
    57  	assertEqual(expected, content, t)
    58  }
    59  
    60  func BenchmarkBlogo(b *testing.B) {
    61  }
    62  
    63  func TestTemplateDir(t *testing.T) {
    64  	_, f, _, _ := runtime.Caller(0)
    65  	expected := filepath.Join(filepath.Dir(f), "templates")
    66  	actual := templateDir()
    67  	assertEqual(expected, actual, t)
    68  }
    69  
    70  func assertEqual(s1, s2 string, t *testing.T) {
    71  	if s1 != s2 {
    72  		t.Errorf("`%s` != `%s`", s1, s2)
    73  	}
    74  }