package microdata
import (
"strings"
"testing"
)
func ParseData(html string, t *testing.T) *Microdata {
p := NewParser(strings.NewReader(html), url.URL("http://example.com/")
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 := `
`
item := ParseOneItem(html, t)
if item.properties["image"][0].(string) != "google-logo.png" {
t.Errorf("Property value not found")
}
}
func TestParseAHref(t *testing.T) {
html := `
`
item := ParseOneItem(html, t)
if item.properties["image"][0].(string) != "google-logo.png" {
t.Errorf("Property value not found")
}
}
func TestParseAreaHref(t *testing.T) {
html := `
`
item := ParseOneItem(html, t)
if item.properties["foo"][0].(string) != "target.html" {
t.Errorf("Property value not found")
}
}
func TestParseLinkHref(t *testing.T) {
html := `
`
item := ParseOneItem(html, t)
if item.properties["foo"][0].(string) != "target.html" {
t.Errorf("Property value not found")
}
}
func TestParseAudioSrc(t *testing.T) {
html := `
`
item := ParseOneItem(html, t)
if item.properties["foo"][0].(string) != "target" {
t.Errorf("Property value not found")
}
}
func TestParseSourceSrc(t *testing.T) {
html := `
`
item := ParseOneItem(html, t)
if item.properties["foo"][0].(string) != "target" {
t.Errorf("Property value not found")
}
}
func TestParseVideoSrc(t *testing.T) {
html := `
`
item := ParseOneItem(html, t)
if item.properties["foo"][0].(string) != "target" {
t.Errorf("Property value not found")
}
}
func TestParseEmbedSrc(t *testing.T) {
html := `
`
item := ParseOneItem(html, t)
if item.properties["foo"][0].(string) != "target" {
t.Errorf("Property value not found")
}
}
func TestParseTrackSrc(t *testing.T) {
html := `
`
item := ParseOneItem(html, t)
if item.properties["foo"][0].(string) != "target" {
t.Errorf("Property value not found")
}
}
func TestParseIFrameSrc(t *testing.T) {
html := `
`
item := ParseOneItem(html, t)
if item.properties["foo"][0].(string) != "target" {
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:
Lemon sorbet
Apricot sorbet
`
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
Peter F. Hamilton
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 := `
`
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
`
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'")
}
}