Skip to content

Commit

Permalink
Gleam format
Browse files Browse the repository at this point in the history
  • Loading branch information
philipgiuliani committed Dec 6, 2023
1 parent 457b397 commit 908b033
Showing 1 changed file with 137 additions and 126 deletions.
263 changes: 137 additions & 126 deletions test/glubs/webvtt_test.gleam
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import gleeunit/should
import gleam/option.{None, Some}
import glubs/webvtt.{Cue,
EndTag, Note, StartTag, Style, Text, Timestamp, WebVTT}
import glubs/webvtt.{Cue, EndTag, Note, StartTag, Style, Text, Timestamp, WebVTT}
import simplifile

pub fn parse_invalid_header_test() {
Expand All @@ -25,11 +24,9 @@ pub fn parse_only_header_test() {
pub fn parse_header_with_comment_test() {
"WEBVTT This is a comment"
|> webvtt.parse()
|> should.equal(Ok(WebVTT(
comment: Some("This is a comment"),
metadata: [],
items: [],
)))
|> should.equal(
Ok(WebVTT(comment: Some("This is a comment"), metadata: [], items: [])),
)
}

pub fn parse_header_with_metadata_test() {
Expand Down Expand Up @@ -67,125 +64,133 @@ pub fn parse_header_with_timestamp_map_test() {
pub fn parse_cue_test() {
"WEBVTT\n\n1\n00:00.123 --> 00:00.456\nTest"
|> webvtt.parse()
|> should.equal(Ok(WebVTT(
comment: None,
metadata: [],
items: [
Cue(
id: Some("1"),
start_time: 123,
end_time: 456,
payload: "Test",
settings: [],
),
],
)))
|> should.equal(
Ok(
WebVTT(comment: None, metadata: [], items: [
Cue(
id: Some("1"),
start_time: 123,
end_time: 456,
payload: "Test",
settings: [],
),
]),
),
)
}

pub fn parse_comment_test() {
let assert Ok(content) = simplifile.read("test/fixtures/webvtt/comments.vtt")

content
|> webvtt.parse()
|> should.equal(Ok(WebVTT(
comment: Some("- Translation of that film I like"),
metadata: [],
items: [
Note(
"This translation was done by Kyle so that\nsome friends can watch it with their parents.",
),
Cue(
id: Some("1"),
start_time: 135_000,
end_time: 140_000,
payload: "- Ta en kopp varmt te.\n- Det är inte varmt.",
settings: [],
),
Cue(
id: Some("2"),
start_time: 140_000,
end_time: 145_000,
payload: "- Har en kopp te.\n- Det smakar som te.",
settings: [],
),
Note("This last line may not translate well."),
Cue(
id: Some("3"),
start_time: 145_000,
end_time: 150_000,
payload: "- Ta en kopp",
settings: [],
|> should.equal(
Ok(
WebVTT(
comment: Some("- Translation of that film I like"),
metadata: [],
items: [
Note(
"This translation was done by Kyle so that\nsome friends can watch it with their parents.",
),
Cue(
id: Some("1"),
start_time: 135_000,
end_time: 140_000,
payload: "- Ta en kopp varmt te.\n- Det är inte varmt.",
settings: [],
),
Cue(
id: Some("2"),
start_time: 140_000,
end_time: 145_000,
payload: "- Har en kopp te.\n- Det smakar som te.",
settings: [],
),
Note("This last line may not translate well."),
Cue(
id: Some("3"),
start_time: 145_000,
end_time: 150_000,
payload: "- Ta en kopp",
settings: [],
),
],
),
],
)))
),
)
}

pub fn parse_style_test() {
let assert Ok(content) = simplifile.read("test/fixtures/webvtt/style.vtt")

content
|> webvtt.parse()
|> should.equal(Ok(WebVTT(
comment: None,
metadata: [],
items: [
Style(
"::cue {\n background-image: linear-gradient(to bottom, dimgray, lightgray);\n color: papayawhip;\n}",
),
Note("comment blocks can be used between style blocks."),
Style("::cue(b) {\n color: peachpuff;\n}"),
Cue(
id: None,
start_time: 0,
end_time: 10_000,
payload: "- Hello <b>world</b>.",
settings: [],
),
Note("style blocks cannot appear after the first cue."),
],
)))
|> should.equal(
Ok(
WebVTT(comment: None, metadata: [], items: [
Style(
"::cue {\n background-image: linear-gradient(to bottom, dimgray, lightgray);\n color: papayawhip;\n}",
),
Note("comment blocks can be used between style blocks."),
Style("::cue(b) {\n color: peachpuff;\n}"),
Cue(
id: None,
start_time: 0,
end_time: 10_000,
payload: "- Hello <b>world</b>.",
settings: [],
),
Note("style blocks cannot appear after the first cue."),
]),
),
)
}

pub fn parse_settings_test() {
let assert Ok(content) = simplifile.read("test/fixtures/webvtt/settings.vtt")

content
|> webvtt.parse()
|> should.equal(Ok(WebVTT(
comment: None,
metadata: [],
items: [
Cue(
id: None,
start_time: 0,
end_time: 4000,
payload: "Where did he go?",
settings: [
#("position", "10%,line-left"),
#("align", "left"),
#("size", "35%"),
],
),
Cue(
id: None,
start_time: 3000,
end_time: 6500,
payload: "I think he went down this lane.",
settings: [#("position", "90%"), #("align", "right"), #("size", "35%")],
),
Cue(
id: None,
start_time: 4000,
end_time: 6500,
payload: "What are you waiting for?",
settings: [
#("position", "45%,line-right"),
#("align", "center"),
#("size", "35%"),
],
),
],
)))
|> should.equal(
Ok(
WebVTT(comment: None, metadata: [], items: [
Cue(
id: None,
start_time: 0,
end_time: 4000,
payload: "Where did he go?",
settings: [
#("position", "10%,line-left"),
#("align", "left"),
#("size", "35%"),
],
),
Cue(
id: None,
start_time: 3000,
end_time: 6500,
payload: "I think he went down this lane.",
settings: [
#("position", "90%"),
#("align", "right"),
#("size", "35%"),
],
),
Cue(
id: None,
start_time: 4000,
end_time: 6500,
payload: "What are you waiting for?",
settings: [
#("position", "45%,line-right"),
#("align", "center"),
#("size", "35%"),
],
),
]),
),
)
}

pub fn to_string_test() {
Expand Down Expand Up @@ -235,36 +240,42 @@ pub fn tokenize_text_test() {
pub fn tokenize_voice_test() {
"<v.loud.shout Rob>Hello</v>"
|> webvtt.tokenize()
|> should.equal(Ok([
StartTag("v", classes: ["loud", "shout"], annotation: Some("Rob")),
Text("Hello"),
EndTag("v"),
]))
|> should.equal(
Ok([
StartTag("v", classes: ["loud", "shout"], annotation: Some("Rob")),
Text("Hello"),
EndTag("v"),
]),
)
}

pub fn timestamp_tag_test() {
"Hello <00:19.500>Phil. <01:00:00.500>How are you?"
|> webvtt.tokenize()
|> should.equal(Ok([
Text("Hello "),
Timestamp(19_500),
Text("Phil. "),
Timestamp(3_600_500),
Text("How are you?"),
]))
|> should.equal(
Ok([
Text("Hello "),
Timestamp(19_500),
Text("Phil. "),
Timestamp(3_600_500),
Text("How are you?"),
]),
)
}

pub fn complex_test() {
"<v Phil>Hi!\n<v.loud.shout Rob>Hello <i>mate!</i></v>"
|> webvtt.tokenize()
|> should.equal(Ok([
StartTag("v", classes: [], annotation: Some("Phil")),
Text("Hi!\n"),
StartTag("v", classes: ["loud", "shout"], annotation: Some("Rob")),
Text("Hello "),
StartTag("i", classes: [], annotation: None),
Text("mate!"),
EndTag("i"),
EndTag("v"),
]))
|> should.equal(
Ok([
StartTag("v", classes: [], annotation: Some("Phil")),
Text("Hi!\n"),
StartTag("v", classes: ["loud", "shout"], annotation: Some("Rob")),
Text("Hello "),
StartTag("i", classes: [], annotation: None),
Text("mate!"),
EndTag("i"),
EndTag("v"),
]),
)
}

0 comments on commit 908b033

Please sign in to comment.