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

feat(parser/renderer/types): Support inline and image icons #613

Merged
merged 7 commits into from
Jun 16, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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
321 changes: 321 additions & 0 deletions pkg/parser/icon_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,321 @@
package parser_test

import (
"github.com/bytesparadise/libasciidoc/pkg/types"
. "github.com/bytesparadise/libasciidoc/testsupport"
. "github.com/onsi/ginkgo" //nolint golint
. "github.com/onsi/gomega" //nolint golint
)

var _ = Describe("icons", func() {

Context("inline icons", func() {

Context("draft document", func() {

It("inline icon with empty alt only", func() {
source := "icon:tip[]"
expected := types.DraftDocument{
Blocks: []interface{}{
types.Paragraph{
Lines: [][]interface{}{
{
types.Icon{
Class: "tip",
},
},
},
},
},
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
})

It("inline icon with empty alt and trailing spaces", func() {
source := "icon:note[] \t\t "
expected := types.DraftDocument{
Blocks: []interface{}{
types.Paragraph{
Lines: [][]interface{}{
{
types.Icon{
Class: "note",
},
types.StringElement{
Content: " \t\t ",
},
},
},
},
},
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
})

It("inline icon with empty alt surrounded by text", func() {
source := "beware icon:caution[] of tigers"
expected := types.DraftDocument{
Blocks: []interface{}{
types.Paragraph{
Lines: [][]interface{}{
{
types.StringElement{
Content: "beware ",
},
types.Icon{
Class: "caution",
},
types.StringElement{
Content: " of tigers",
},
},
},
},
},
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
})

It("inline icon with size alone", func() {
source := "icon:caution[2x]"
expected := types.DraftDocument{
Blocks: []interface{}{
types.Paragraph{
Lines: [][]interface{}{
{
types.Icon{
Class: "caution",
Attributes: types.Attributes{types.AttrIconSize: "2x"},
},
},
},
},
},
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
})

It("inline icon with other attribute (title)", func() {
source := "icon:caution[title=\"bogus\"]"
expected := types.DraftDocument{
Blocks: []interface{}{
types.Paragraph{
Lines: [][]interface{}{
{
types.Icon{
Class: "caution",
Attributes: types.Attributes{types.AttrImageTitle: "bogus"},
},
},
},
},
},
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
})

It("inline icon with anchor attribute", func() {
source := "icon:caution[id=anchor]"
expected := types.DraftDocument{
Blocks: []interface{}{
types.Paragraph{
Lines: [][]interface{}{
{
types.Icon{
Class: "caution",
Attributes: types.Attributes{
types.AttrID: "anchor",
types.AttrCustomID: true,
},
},
},
},
},
},
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
})

It("inline icon with multiple other attributes", func() {
source := "icon:caution[id=anchor,title=\"White Fang\"]"
expected := types.DraftDocument{
Blocks: []interface{}{
types.Paragraph{
Lines: [][]interface{}{
{
types.Icon{
Class: "caution",
Attributes: types.Attributes{
types.AttrID: "anchor",
types.AttrCustomID: true,
types.AttrImageTitle: "White Fang",
},
},
},
},
},
},
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
})

It("inline icon with size and multiple other attributes", func() {
source := "icon:caution[fw,id=anchor,title=\"White Fang\"]"
expected := types.DraftDocument{
Blocks: []interface{}{
types.Paragraph{
Lines: [][]interface{}{
{
types.Icon{
Class: "caution",
Attributes: types.Attributes{
types.AttrID: "anchor",
types.AttrCustomID: true,
types.AttrImageTitle: "White Fang",
types.AttrIconSize: "fw",
},
},
},
},
},
},
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
})

It("inline icon with space after colon", func() {
source := "here is my icon: icon:info[]"
expected := types.DraftDocument{
Blocks: []interface{}{
types.Paragraph{
Lines: [][]interface{}{
{
types.StringElement{
Content: "here is my icon: ",
},
types.Icon{
Class: "info",
},
},
},
},
},
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
})

It("inline icon in title works", func() {
source := `== a icon:note[] from me`
expected := types.DraftDocument{
Blocks: []interface{}{
types.Section{
Level: 1,
Title: []interface{}{
types.StringElement{
Content: "a ",
},
types.Icon{
Class: "note",
},
types.StringElement{
Content: " from me",
},
},
Elements: []interface{}{},
},
},
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
})

It("inline icon at title start", func() {
source := `= icon:warning[] or what icon:note[] to do`
expected := types.DraftDocument{
Blocks: []interface{}{
types.Section{
Level: 0,
Title: []interface{}{
types.Icon{Class: "warning"},
types.StringElement{Content: " or what "},
types.Icon{Class: "note"},
types.StringElement{Content: " to do"},
},
Elements: []interface{}{},
},
},
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
})

// NB: The existing grammar for labeled list items does not support any markup
// in the term text.
It("inline icon as labeled list item description", func() {
source := `discount:: icon:tags[alt="Discount"] Cheap cheap!
item 2:: two`
expected := types.DraftDocument{
Blocks: []interface{}{

types.LabeledListItem{
Level: 1,
Term: []interface{}{
types.StringElement{Content: "discount"},
},
Elements: []interface{}{
types.Paragraph{
Lines: [][]interface{}{
{
types.Icon{Class: "tags", Attributes: types.Attributes{types.AttrImageAlt: "Discount"}},
types.StringElement{Content: " Cheap cheap!"},
},
},
},
},
},
types.LabeledListItem{
Level: 1,
Term: []interface{}{
types.StringElement{Content: "item 2"},
},
Elements: []interface{}{
types.Paragraph{
Lines: [][]interface{}{
{
types.StringElement{Content: "two"},
},
},
},
},
},
},
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))

})

It("inline icon in quoted text", func() {
source := `an _italicized icon:warning[] message_`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry to insist, but could you add a test for each kind of quoted text? (bold, monospace, etc.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh. Ok. The parser tests are a royal pain. I have come to really despise the test framework -- it is almost impossible to read the output when there is an error sometimes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can try something like this:

ginkgo -focus "my test" pkg/parser -- -debug

with the -debug flag passed to the test, you'll have a dump of the actual result, so it's sometimes easier to compare with the expectation. Sometimes the diff is enough, but not always

expected := types.DraftDocument{
Blocks: []interface{}{
types.Paragraph{
Lines: [][]interface{}{
{
types.StringElement{Content: "an "},
types.QuotedText{
Kind: types.Italic,
Elements: []interface{}{
types.StringElement{Content: "italicized "},
types.Icon{Class: "warning"},
types.StringElement{Content: " message"},
},
},
},
},
},
},
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
})

})
})
})
Loading