package microdata import ( "strings" "testing" ) func ParseData(html string, t *testing.T) *Microdata { p := NewParser(strings.NewReader(html)) 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) != "google-logo.png" { t.Errorf("Property value not found") } } func TestParseAHref(t *testing.T) { html := `
foo
` 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:

` 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) } }