Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add test for chinese character formatting bug #249

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion glamour_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package glamour
import (
"bytes"
"io/ioutil"
"path/filepath"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
)

const (
Expand Down Expand Up @@ -42,7 +45,7 @@ func TestTermRendererWriter(t *testing.T) {

// generate
if generate {
err := ioutil.WriteFile(testFile, b, 0644)
err := ioutil.WriteFile(testFile, b, 0o644)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -218,3 +221,47 @@ func TestCapitalization(t *testing.T) {
t.Errorf("Rendered output doesn't match!\nExpected: `\n%s`\nGot: `\n%s`\n", td, b)
}
}

func TestWrapping(t *testing.T) {
langDir := "testdata/lang-support/"
files, err := filepath.Glob(langDir + "*.md")
if err != nil {
t.Fatal(err)
}
if len(files) == 0 {
t.Fatal("No files found, please resolve paths before trying again.")
}

for _, f := range files {
bn := strings.TrimSuffix(filepath.Base(f), ".md")
goldpath := filepath.Join(langDir, bn+".test")

t.Run(bn, func(t *testing.T) {
r, err := NewTermRenderer(
WithStyles(DarkStyleConfig),
WithWordWrap(80),
)
if err != nil {
t.Fatal(err)
}
// get markdown contents
in, err := ioutil.ReadFile(f)
if err != nil {
t.Fatal(err)
}
got, err := r.RenderBytes(in)
if err != nil {
t.Fatal(err)
}
// get desired contents
want, err := ioutil.ReadFile(goldpath)
if err != nil {
t.Fatal(err)
}

if diff := cmp.Diff(string(got), string(want)); diff != "" {
t.Fatalf("got != want\n-want +got:\ndiff:\n%s", diff)
}
})
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.17

require (
github.com/alecthomas/chroma/v2 v2.8.0
github.com/google/go-cmp v0.5.9
github.com/microcosm-cc/bluemonday v1.0.25
github.com/muesli/reflow v0.3.0
github.com/muesli/termenv v0.15.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuP
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E=
github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
Expand Down
454 changes: 454 additions & 0 deletions testdata/lang-support/long-chinese-text.md

Large diffs are not rendered by default.

454 changes: 454 additions & 0 deletions testdata/lang-support/long-chinese-text.test

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions testdata/lang-support/long-latin-test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Lorem Ipsum

## Section 1.10.32 of "de Finibus Bonorum et Malorum", written by Cicero in 45 BC
"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?"

## 1914 translation by H. Rackham
"But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?"
35 changes: 35 additions & 0 deletions testdata/lang-support/long-latin-test.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

  Lorem Ipsum                                                                
                                                                             
 ## Section 1.10.32 of "de Finibus Bonorum et Malorum", written by Cicero in 
 45 BC                                                                       
                                                                             
 "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium 
 doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore 
 veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim  
 ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia  
 consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque 
 porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur,     
 adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et
 dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis    
 nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid  
 ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea  
 voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem
 eum fugiat quo voluptas nulla pariatur?"                                    
                                                                             
 ## 1914 translation by H. Rackham                                           
                                                                             
 "But I must explain to you how all this mistaken idea of denouncing pleasure
 and praising pain was born and I will give you a complete account of the    
 system, and expound the actual teachings of the great explorer of the truth,
 the master-builder of human happiness. No one rejects, dislikes, or avoids  
 pleasure itself, because it is pleasure, but because those who do not know  
 how to pursue pleasure rationally encounter consequences that are extremely 
 painful. Nor again is there anyone who loves or pursues or desires to obtain
 pain of itself, because it is pain, but because occasionally circumstances  
 occur in which toil and pain can procure him some great pleasure. To take a 
 trivial example, which of us ever undertakes laborious physical exercise,   
 except to obtain some advantage from it? But who has any right to find fault
 with a man who chooses to enjoy a pleasure that has no annoying             
 consequences, or one who avoids a pain that produces no resultant pleasure?"

1 change: 1 addition & 0 deletions testdata/lang-support/short-chinese-text.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
李绅 《悯农》锄禾日当午,汗滴禾下土。谁知盘中餐,粒粒皆辛苦。床前明月光,疑是地上霜。举头望明月,低头思故乡。
4 changes: 4 additions & 0 deletions testdata/lang-support/short-chinese-text.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

 李绅                                                                        
 《悯农》锄禾日当午,汗滴禾下土。谁知盘中餐,粒粒皆辛苦。床前明月光,疑是地上霜。举头望明月,低头思故乡。

Loading