package microdata import ( "bytes" "net/url" "strings" "testing" ) func ParseData(html string, t *testing.T) *Microdata { u, _ := url.Parse("http://example.com/") p := NewParser(strings.NewReader(html), u) data, err := p.Parse() if err != nil { t.Errorf("Expected no error but got %d", err) } if data == nil { t.Errorf("Expected non-nil data") } return data } func ParseOneItem(html string, t *testing.T) *Item { data := ParseData(html, t) return data.Items[0] } func TestParse(t *testing.T) { html := `

My name is Elizabeth.

` item := ParseOneItem(html, t) if item.Properties["name"][0].(string) != "Elizabeth" { t.Errorf("Property value not found") } } func TestParseActuallyParses(t *testing.T) { html := `

My name is Daniel.

` item := ParseOneItem(html, t) if item.Properties["name"][0].(string) != "Daniel" { t.Errorf("Property value not found") } } func TestParseThreeProps(t *testing.T) { html := `

My name is Neil.

My band is called Four Parts Water.

I am British.

` item := ParseOneItem(html, t) if item.Properties["name"][0].(string) != "Neil" { t.Errorf("Property value not found") } if item.Properties["band"][0].(string) != "Four Parts Water" { t.Errorf("Property value not found") } if item.Properties["nationality"][0].(string) != "British" { t.Errorf("Property value not found") } } func TestParseImgSrc(t *testing.T) { html := `
Google
` item := ParseOneItem(html, t) if item.Properties["image"][0].(string) != "http://example.com/foo" { t.Errorf("Property value not found") } } func TestParseAHref(t *testing.T) { html := `
foo
` item := ParseOneItem(html, t) if item.Properties["image"][0].(string) != "http://example.com/foo" { t.Errorf("Property value not found") } } func TestParseAreaHref(t *testing.T) { html := `
` item := ParseOneItem(html, t) if item.Properties["foo"][0].(string) != "http://example.com/foo" { t.Errorf("Property value not found") } } func TestParseLinkHref(t *testing.T) { html := `
` item := ParseOneItem(html, t) if item.Properties["foo"][0].(string) != "http://example.com/foo" { t.Errorf("Property value not found") } } func TestParseAudioSrc(t *testing.T) { html := `
` item := ParseOneItem(html, t) if item.Properties["foo"][0].(string) != "http://example.com/foo" { t.Errorf("Property value not found") } } func TestParseSourceSrc(t *testing.T) { html := `
` item := ParseOneItem(html, t) if item.Properties["foo"][0].(string) != "http://example.com/foo" { t.Errorf("Property value not found") } } func TestParseVideoSrc(t *testing.T) { html := `
` item := ParseOneItem(html, t) if item.Properties["foo"][0].(string) != "http://example.com/foo" { t.Errorf("Property value not found") } } func TestParseEmbedSrc(t *testing.T) { html := `
` item := ParseOneItem(html, t) if item.Properties["foo"][0].(string) != "http://example.com/foo" { t.Errorf("Property value not found") } } func TestParseTrackSrc(t *testing.T) { html := `
` item := ParseOneItem(html, t) if item.Properties["foo"][0].(string) != "http://example.com/foo" { t.Errorf("Property value not found") } } func TestParseIFrameSrc(t *testing.T) { html := `
` item := ParseOneItem(html, t) if item.Properties["foo"][0].(string) != "http://example.com/foo" { t.Errorf("Property value not found") } } func TestParseDataValue(t *testing.T) { html := `

The Instigator 2000

` item := ParseOneItem(html, t) if item.Properties["product-id"][0].(string) != "9678AOU879" { t.Errorf("Property value not found") } } func TestParseTimeDatetime(t *testing.T) { html := `

I was born on .

` item := ParseOneItem(html, t) if item.Properties["birthday"][0].(string) != "2009-05-10" { t.Errorf("Property value not found") } } func TestParseTwoValues(t *testing.T) { html := `

Flavors in my favorite ice cream:

` item := ParseOneItem(html, t) if len(item.Properties["flavor"]) != 2 { t.Errorf("Expecting 2 values but got %d", len(item.Properties["flavor"])) } if item.Properties["flavor"][0].(string) != "Lemon sorbet" { t.Errorf("Property value 'Lemon sorbet' not found") } if item.Properties["flavor"][1].(string) != "Apricot sorbet" { t.Errorf("Property value 'Apricot sorbet' not found") } } func TestParseTwoPropertiesOneValue(t *testing.T) { html := `
orange
` item := ParseOneItem(html, t) if len(item.Properties) != 2 { t.Errorf("Expecting 2 properties but got %d", len(item.Properties)) } if len(item.Properties["favorite-color"]) != 1 { t.Errorf("Expecting 1 value but got %d", len(item.Properties["favorite-color"])) } if len(item.Properties["favorite-fruit"]) != 1 { t.Errorf("Expecting 1 value but got %d", len(item.Properties["favorite-fruit"])) } if item.Properties["favorite-color"][0].(string) != "orange" { t.Errorf("Property value 'orange' not found for 'favorite-color'") } if item.Properties["favorite-fruit"][0].(string) != "orange" { t.Errorf("Property value 'orange' not found for 'favorite-fruit'") } } func TestParseTwoPropertiesOneValueMultispaced(t *testing.T) { html := `
orange
` item := ParseOneItem(html, t) if len(item.Properties) != 2 { t.Errorf("Expecting 2 properties but got %d", len(item.Properties)) } if len(item.Properties["favorite-color"]) != 1 { t.Errorf("Expecting 1 value but got %d", len(item.Properties["favorite-color"])) } if len(item.Properties["favorite-fruit"]) != 1 { t.Errorf("Expecting 1 value but got %d", len(item.Properties["favorite-fruit"])) } if item.Properties["favorite-color"][0].(string) != "orange" { t.Errorf("Property value 'orange' not found for 'favorite-color'") } if item.Properties["favorite-fruit"][0].(string) != "orange" { t.Errorf("Property value 'orange' not found for 'favorite-fruit'") } } func TestParseItemType(t *testing.T) { html := `

Hedral

` item := ParseOneItem(html, t) if len(item.Types) != 1 { t.Errorf("Expecting 1 type but got %d", len(item.Types)) } if item.Types[0] != "http://example.org/animals#cat" { t.Errorf("Expecting type of 'http://example.org/animals#cat' but got %d", item.Types[0]) } } func TestParseMultipleItemTypes(t *testing.T) { html := `

Hedral

` item := ParseOneItem(html, t) if len(item.Types) != 2 { t.Errorf("Expecting 2 types but got %d", len(item.Types)) } if item.Types[0] != "http://example.org/animals#mammal" { t.Errorf("Expecting type of 'http://example.org/animals#mammal' but got %d", item.Types[0]) } if item.Types[1] != "http://example.org/animals#cat" { t.Errorf("Expecting type of 'http://example.org/animals#cat' but got %d", item.Types[1]) } } func TestParseItemId(t *testing.T) { html := `
Title
The Reality Dysfunction
Author
Publication date
` item := ParseOneItem(html, t) if item.ID != "urn:isbn:0-330-34032-8" { t.Errorf("Expecting id of 'urn:isbn:0-330-34032-8' but got %d", item.ID) } } func TestParseItemRef(t *testing.T) { html := `

A white house, boarded up, sits in a forest.
The house I found.

All images licensed under the MIT license.

` item := ParseOneItem(html, t) if len(item.Properties) != 3 { t.Errorf("Expecting 3 properties but got %d", len(item.Properties)) } if item.Properties["license"][0].(string) != "http://www.opensource.org/licenses/mit-license.php" { t.Errorf("Property value 'http://www.opensource.org/licenses/mit-license.php' not found for 'license'") } } func TestParseSharedItemRef(t *testing.T) { html := ` Photo gallery

My photos

A white house, boarded up, sits in a forest.
The house I found.
Outside the house is a mailbox. It has a leaflet inside.
The mailbox.
` data := ParseData(html, t) if len(data.Items) != 2 { t.Errorf("Expecting 2 items but got %d", len(data.Items)) } if len(data.Items[0].Properties) != 3 { t.Errorf("Expecting 3 properties but got %d", len(data.Items[0].Properties)) } if len(data.Items[1].Properties) != 3 { t.Errorf("Expecting 3 properties but got %d", len(data.Items[1].Properties)) } if data.Items[0].Properties["license"][0].(string) != "http://www.opensource.org/licenses/mit-license.php" { t.Errorf("Property value 'http://www.opensource.org/licenses/mit-license.php' not found for 'license'") } if data.Items[1].Properties["license"][0].(string) != "http://www.opensource.org/licenses/mit-license.php" { t.Errorf("Property value 'http://www.opensource.org/licenses/mit-license.php' not found for 'license'") } } func TestParseMultiValuedItemRef(t *testing.T) { html := `

Name: Amanda

Age: 26

` data := ParseData(html, t) if data.Items[0].Properties["name"][0].(string) != "Amanda" { t.Errorf("Property value 'Amanda' not found for 'name'") } if data.Items[0].Properties["age"][0].(string) != "26" { t.Errorf("Property value '26' not found for 'age'") } } func TestParseEmbeddedItem(t *testing.T) { html := `

Name: Amanda

Band: Jazz Band (12 players)

` data := ParseData(html, t) if len(data.Items) != 1 { t.Errorf("Expecting 1 item but got %d", len(data.Items)) } if data.Items[0].Properties["name"][0].(string) != "Amanda" { t.Errorf("Property value 'Amanda' not found for 'name'") } subitem := data.Items[0].Properties["band"][0].(*Item) if subitem.Properties["name"][0].(string) != "Jazz Band" { t.Errorf("Property value 'Jazz Band' not found for 'name'") } } func TestParseEmbeddedItemWithItemRef(t *testing.T) { html := `

Name: Amanda

Band: Jazz Band

Size: 12 players

` data := ParseData(html, t) if len(data.Items) != 1 { t.Errorf("Expecting 1 item but got %d", len(data.Items)) } if data.Items[0].Properties["name"][0].(string) != "Amanda" { t.Errorf("Property value 'Amanda' not found for 'name'") } subitem := data.Items[0].Properties["band"][0].(*Item) if subitem.Properties["name"][0].(string) != "Jazz Band" { t.Errorf("Property value 'Jazz Band' not found for 'name'") } } func TestParseRelativeURL(t *testing.T) { html := `
foo
` item := ParseOneItem(html, t) if item.Properties["image"][0].(string) != "http://example.com/test.png" { t.Errorf("Property value not found") } } func TestParseItemRelativeId(t *testing.T) { html := `
Title
The Reality Dysfunction
Author
Publication date
` item := ParseOneItem(html, t) if item.ID != "http://example.com/foo" { t.Errorf("Expecting id of 'http://example.com/foo' but got %d", item.ID) } } func TestJson(t *testing.T) { item := NewItem() item.SetString("name", "Elizabeth") data := NewMicrodata() data.AddItem(item) expected := []byte(`{"items":[{"properties":{"name":["Elizabeth"]}}]}`) actual, _ := data.Json() if !bytes.Equal(actual, expected) { t.Errorf("Expecting %s but got %s", expected, actual) } } func TestJsonWithType(t *testing.T) { item := NewItem() item.AddType("http://example.org/animals#cat") item.SetString("name", "Elizabeth") data := NewMicrodata() data.AddItem(item) expected := []byte(`{"items":[{"properties":{"name":["Elizabeth"]},"type":["http://example.org/animals#cat"]}]}`) actual, _ := data.Json() if !bytes.Equal(actual, expected) { t.Errorf("Expecting %s but got %s", expected, actual) } }