From 63c3acd5f9a2400959cb83b85d671516191213c2 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Sun, 15 Dec 2019 05:30:45 +0100 Subject: [PATCH] Test TermRenderer with testdata/readme.markdown.in --- gold_test.go | 44 +++++++++++++++++++++++++++------- testdata/readme.markdown.in | 36 ++++++++++++++++++++++++++++ testdata/readme.test | 48 +++++++++++++++++++++++++++++++++++++ 3 files changed, 120 insertions(+), 8 deletions(-) create mode 100644 testdata/readme.markdown.in create mode 100644 testdata/readme.test diff --git a/gold_test.go b/gold_test.go index 807f95c..9f4a463 100644 --- a/gold_test.go +++ b/gold_test.go @@ -1,35 +1,63 @@ package gold import ( + "bytes" "io/ioutil" "testing" "github.com/charmbracelet/gold/ansi" ) +const ( + generate = false + markdown = "testdata/readme.markdown.in" + testFile = "testdata/readme.test" +) + func TestTermRenderer(t *testing.T) { - r, err := NewTermRenderer("notty", ansi.Options{ + r, err := NewTermRenderer("styles/dark.json", ansi.Options{ WordWrap: 80, }) if err != nil { t.Fatal(err) } - expLen := 166 - _, err = r.Write([]byte("# Test")) + in, err := ioutil.ReadFile(markdown) if err != nil { - t.Error(err) + t.Fatal(err) + } + + _, err = r.Write(in) + if err != nil { + t.Fatal(err) } err = r.Close() if err != nil { - t.Error(err) + t.Fatal(err) } b, err := ioutil.ReadAll(r) if err != nil { - t.Error(err) + t.Fatal(err) } - if len(b) != expLen { - t.Errorf("Expected rendered content with a length of %d, got %d!", expLen, len(b)) + + // generate + if generate { + err := ioutil.WriteFile(testFile, b, 0644) + if err != nil { + t.Fatal(err) + } + return + } + + // verify + td, err := ioutil.ReadFile(testFile) + if err != nil { + t.Fatal(err) + } + + if !bytes.Equal(td, b) { + t.Errorf("Rendered output doesn't match!\nExpected: `\n%s`\nGot: `\n%s`\n", + string(td), b) } } diff --git a/testdata/readme.markdown.in b/testdata/readme.markdown.in new file mode 100644 index 0000000..15f8974 --- /dev/null +++ b/testdata/readme.markdown.in @@ -0,0 +1,36 @@ +# Gold + +Render markdown on the CLI, with _pizzazz_! + +## What is it? + +Gold is a Golang library that allows you to use JSON based stylesheets to +render Markdown files in the terminal. Just like CSS, you can define color and +style attributes on Markdown elements. The difference is that you use ANSI +color and terminal codes instead of CSS properties and hex colors. + +## Usage + +See [cmd/gold](cmd/gold/). + +## Example Output + +![Gold Dark Style](https://github.com/charmbracelet/gold/raw/master/styles/gallery/dark.png) + +Check out the [Gold Style Gallery](https://github.com/charmbracelet/gold/blob/master/styles/gallery/README.md)! + +## Colors + +Currently `gold` uses the [Aurora ANSI colors](https://godoc.org/github.com/logrusorgru/aurora#Index). + +## Development + +Style definitions located in `styles/` can be embedded into the binary by +running [statik](https://github.com/rakyll/statik): + +```console +statik -f -src styles -include "*.json" +``` + +You can re-generate screenshots of all available styles by running `gallery.sh`. +This requires `termshot` and `pngcrush` installed on your system! diff --git a/testdata/readme.test b/testdata/readme.test new file mode 100644 index 0000000..7222587 --- /dev/null +++ b/testdata/readme.test @@ -0,0 +1,48 @@ + + +  Gold                                                                        +                                                                              +                                                                              + Render markdown on the CLI, with pizzazz!                                    +                                                                              +                                                                              + ## What is it?                                                               +                                                                              + Gold is a Golang library that allows you to use JSON based stylesheets to    + render Markdown files in the terminal. Just like CSS, you can define color   + and style attributes on Markdown elements. The difference is that you use    + ANSI color and terminal codes instead of CSS properties and hex colors.      +                                                                              +                                                                              + ## Usage                                                                     +                                                                              + See cmd/gold /cmd/gold/.                                                     +                                                                              +                                                                              + ## Example Output                                                            +                                                                              + Image: Gold Dark Style 🡆                                                     + https://github.com/charmbracelet/gold/raw/master/styles/gallery/dark.png     +                                                                              + Check out the Gold Style Gallery                                             + https://github.com/charmbracelet/gold/blob/master/styles/gallery/README.md!  +                                                                              +                                                                              + ## Colors                                                                    +                                                                              + Currently  gold  uses the Aurora ANSI colors                                 + https://godoc.org/github.com/logrusorgru/aurora#Index.                       +                                                                              +                                                                              + ## Development                                                               +                                                                              + Style definitions located in  styles/  can be embedded into the binary by    + running statik https://github.com/rakyll/statik:                             +                                                                              +     statik -f -src styles -include "*.json"                                  +                                                                              + You can re-generate screenshots of all available styles by running           + gallery.sh . This requires  termshot  and  pngcrush  installed on your       + system!                                                                      + +