From bf63c2af833ec82cf26f346acc59580f65a6ecfc Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Thu, 15 Feb 2024 05:43:18 -0800 Subject: [PATCH] initial commits --- appendframeimageaspectratio.go | 32 ++++++++++ appendframeimageaspectratio_test.go | 96 +++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 appendframeimageaspectratio.go create mode 100644 appendframeimageaspectratio_test.go diff --git a/appendframeimageaspectratio.go b/appendframeimageaspectratio.go new file mode 100644 index 0000000..fee0ec4 --- /dev/null +++ b/appendframeimageaspectratio.go @@ -0,0 +1,32 @@ +package frameproto + +// AppendFrameImageAspectRatio will append the HTML element for the Frame-Protocol's (i.e., Farcaster Frame's) "fc:frame:image:aspect_ratio" name-value pair. +// +// For example, this call: +// +// var p []byte +// +// // .. +// +// var aspectRatio string = "1.91:1" +// +// p = frameproto.AppendFrameImageAspectRatio(p, aspectRatio) +// +// Would append this HTML element: +// +// +// +// Note that this package provides some constants to use with AppendFrameImageAspectRatio. +// Namely: AspectRatioOnePointNineOneToOne (for "1.91:1") and AspectRatioOneToOne (for "1:1"). +// +// Which in code would be used at: +// +// p = frameproto.AppendFrameImageAspectRatio(p, frameproto.AspectRatioOnePointNineOneToOne) +// +// And: +// +// p = frameproto.AppendFrameImageAspectRatio(p, frameproto.AspectRatioOneToOne) +func AppendFrameImageAspectRatio(p []byte, label string) []byte { + const property string = MetaPropertyFrameImageAspectRatio + return appendMetaPropertyContent(p, property, label) +} diff --git a/appendframeimageaspectratio_test.go b/appendframeimageaspectratio_test.go new file mode 100644 index 0000000..8debb4a --- /dev/null +++ b/appendframeimageaspectratio_test.go @@ -0,0 +1,96 @@ +package frameproto + +import ( + "testing" +) + +func TestAppendFrameImageAspectRatio(t *testing.T) { + + tests := []struct{ + AspectRatio string + Expected string + }{ + { + AspectRatio: "", + Expected: ``+"\n", + }, + + + + { + AspectRatio: "something", + Expected: ``+"\n", + }, + + + + { + AspectRatio: "Hello world! 🙂", + Expected: ``+"\n", + }, + + + + { + AspectRatio: "1.91:1", + Expected: ``+"\n", + }, + + + + { + AspectRatio: "1:1", + Expected: ``+"\n", + }, + } + + for testNumber, test := range tests { + + { + var buffer [256]byte + var p []byte = buffer[0:0] + + p = AppendFrameImageAspectRatio(p, test.AspectRatio) + + expected := test.Expected + actual := string(p) + + if expected != actual { + t.Errorf("For test #%d, the actual written meta-tag is not what was expected." ,testNumber) + t.Logf("EXPECTED: %s", expected) + t.Logf("ACTUAL: %s", actual) + t.Logf("EXPECTED: %q", expected) + t.Logf("ACTUAL: %q", actual) + t.Logf("AspectRatio: %q", test.AspectRatio) + continue + } + } + + { + const top string = "\n\n" + const bottom string = "\n\n\n\n" + + var buffer [256]byte + var p []byte = buffer[0:0] + + p = append(p, top...) + + p = AppendFrameImageAspectRatio(p, test.AspectRatio) + + p = append(p, bottom...) + + expected := top + test.Expected + bottom + actual := string(p) + + if expected != actual { + t.Errorf("For test #%d, the actual written meta-tag is not what was expected." ,testNumber) + t.Logf("EXPECTED: %s", expected) + t.Logf("ACTUAL: %s", actual) + t.Logf("EXPECTED: %q", expected) + t.Logf("ACTUAL: %q", actual) + t.Logf("AspectRatio: %q", test.AspectRatio) + continue + } + } + } +}