Update implementation to have null properties if they don't exist

This commit is contained in:
Julien Lengrand-Lambert
2025-05-01 13:40:34 +02:00
parent e445fe2e6f
commit b960a63da7
2 changed files with 20 additions and 22 deletions

View File

@@ -50,11 +50,18 @@ class Parser {
println(tags)
println(cleanTags)
// Extract the basic required Open Graph properties
val title = tags.select("meta[property=og:title]").attr("content")
val image = tags.select("meta[property=og:image]").attr("content")
val url = tags.select("meta[property=og:url]").attr("content")
val type = tags.select("meta[property=og:type]").attr("content")
val title =
if (tags.select("meta[property=og:title]").isEmpty()) null
else tags.select("meta[property=og:title]").attr("content")
val image =
if (tags.select("meta[property=og:image]").isEmpty()) null
else tags.select("meta[property=og:image]").attr("content")
val url =
if (tags.select("meta[property=og:url]").isEmpty()) null
else tags.select("meta[property=og:url]").attr("content")
val type =
if (tags.select("meta[property=og:type]").isEmpty()) null
else tags.select("meta[property=og:type]").attr("content")
return OpenGraph(
tags,

View File

@@ -92,21 +92,15 @@ class ParserTest {
val document = fetcher.fromString(incompleteHtml)
val openGraph = parser.extractOpenGraphTags(document)
// Verify that the available property is extracted correctly
assertEquals("The Rock", openGraph.title)
// Verify that missing properties are empty strings
assertEquals("", openGraph.type)
assertEquals("", openGraph.url)
assertEquals("", openGraph.image)
assertEquals(null, openGraph.type)
assertEquals(null, openGraph.url)
assertEquals(null, openGraph.image)
// Verify that the OpenGraph object is not valid due to missing required properties
assertFalse(openGraph.isValid())
// Verify that only the available tags are extracted
assertEquals(2, openGraph.tags.size)
// Verify specific tag content
val descriptionTag = openGraph.tags.find { it.property == "description" }
assertNotNull(descriptionTag)
assertEquals("An action movie about a rock", descriptionTag.content)
@@ -117,16 +111,13 @@ class ParserTest {
val document = fetcher.fromString(noOgHtml)
val openGraph = parser.extractOpenGraphTags(document)
// Verify that all properties are empty strings
assertEquals("", openGraph.title)
assertEquals("", openGraph.type)
assertEquals("", openGraph.url)
assertEquals("", openGraph.image)
// Verify that all properties are null
assertEquals(null, openGraph.title)
assertEquals(null, openGraph.type)
assertEquals(null, openGraph.url)
assertEquals(null, openGraph.image)
// Verify that the OpenGraph object is not valid
assertFalse(openGraph.isValid())
// Verify that no tags are extracted
assertEquals(0, openGraph.tags.size)
}
}