From 2b24feddab842a2c7d9cf962dd954d0cb4776a78 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Wed, 14 Feb 2024 16:27:07 -0800 Subject: [PATCH] initial commits --- appendframeimage.go | 21 +++++++++ appendframeimage_test.go | 96 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 appendframeimage.go create mode 100644 appendframeimage_test.go diff --git a/appendframeimage.go b/appendframeimage.go new file mode 100644 index 0000000..900cc9c --- /dev/null +++ b/appendframeimage.go @@ -0,0 +1,21 @@ +package frameproto + +// AppendFrameImage will append the HTML element for the Frame-Protocol's (i.e., Farcaster Frame's) "fc:frame:image" name-value pair. +// +// For example, this call: +// +// var p []byte +// +// // ... +// +// var url string = "https://example.com/images/screen.png" +// +// p = frameproto.AppendFrameImage(p, url) +// +// Would append this HTML element: +// +// +func AppendFrameImage(p []byte, url string) []byte { + const property string = MetaPropertyFrameImage + return appendMetaPropertyContent(p, property, url) +} diff --git a/appendframeimage_test.go b/appendframeimage_test.go new file mode 100644 index 0000000..b43d6df --- /dev/null +++ b/appendframeimage_test.go @@ -0,0 +1,96 @@ +package frameproto + +import ( + "testing" +) + +func TestAppendFrameImage(t *testing.T) { + + tests := []struct{ + Version string + Expected string + }{ + { + Version: "", + Expected: ``+"\n", + }, + + + + { + Version: "something", + Expected: ``+"\n", + }, + + + + { + Version: "Hello world! 🙂", + Expected: ``+"\n", + }, + + + + { + Version: "https://example.com/path/to/post/to.png", + Expected: ``+"\n", + }, + + + + { + Version: "x-proto:apple/banana/cherry", + Expected: ``+"\n", + }, + } + + for testNumber, test := range tests { + + { + var buffer [256]byte + var p []byte = buffer[0:0] + + p = AppendFrameImage(p, test.Version) + + 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("LABEL: %q", test.Version) + 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 = AppendFrameImage(p, test.Version) + + 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("LABEL: %q", test.Version) + continue + } + } + } +}