package microdata import ( "strings" "testing" ) func ReadData(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 ReadOneItem(html string, t *testing.T) *Item { data := ReadData(html, t) return data.items[0] } func TestRead(t *testing.T) { html := `

My name is Elizabeth.

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

My name is Daniel.

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

My name is Neil.

My band is called Four Parts Water.

I am British.

` item := ReadOneItem(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 TestReadImgSrc(t *testing.T) { html := `
Google
` item := ReadOneItem(html, t) if item.properties["image"][0].(string) != "google-logo.png" { t.Errorf("Property value not found") } } func TestReadAHref(t *testing.T) { html := `
foo
` item := ReadOneItem(html, t) if item.properties["image"][0].(string) != "google-logo.png" { t.Errorf("Property value not found") } } func TestReadAreaHref(t *testing.T) { html := `
` item := ReadOneItem(html, t) if item.properties["foo"][0].(string) != "target.html" { t.Errorf("Property value not found") } } func TestReadLinkHref(t *testing.T) { html := `
` item := ReadOneItem(html, t) if item.properties["foo"][0].(string) != "target.html" { t.Errorf("Property value not found") } } func TestReadAudioSrc(t *testing.T) { html := `
` item := ReadOneItem(html, t) if item.properties["foo"][0].(string) != "target" { t.Errorf("Property value not found") } } func TestReadSourceSrc(t *testing.T) { html := `
` item := ReadOneItem(html, t) if item.properties["foo"][0].(string) != "target" { t.Errorf("Property value not found") } } func TestReadVideoSrc(t *testing.T) { html := `
` item := ReadOneItem(html, t) if item.properties["foo"][0].(string) != "target" { t.Errorf("Property value not found") } } func TestReadEmbedSrc(t *testing.T) { html := `
` item := ReadOneItem(html, t) if item.properties["foo"][0].(string) != "target" { t.Errorf("Property value not found") } } func TestReadTrackSrc(t *testing.T) { html := `
` item := ReadOneItem(html, t) if item.properties["foo"][0].(string) != "target" { t.Errorf("Property value not found") } } func TestReadIFrameSrc(t *testing.T) { html := `
` item := ReadOneItem(html, t) if item.properties["foo"][0].(string) != "target" { t.Errorf("Property value not found") } } func TestReadDataValue(t *testing.T) { html := `

The Instigator 2000

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

I was born on .

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

Flavors in my favorite ice cream:

` item := ReadOneItem(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 TestReadTwoPropertiesOneValue(t *testing.T) { html := `
orange
` item := ReadOneItem(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 TestReadTwoPropertiesOneValueMultispaced(t *testing.T) { html := `
orange
` item := ReadOneItem(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 TestReadItemType(t *testing.T) { html := `

Hedral

` item := ReadOneItem(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 TestReadMultiplrItemTypes(t *testing.T) { html := `

Hedral

` item := ReadOneItem(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]) } }