-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tpl: Add float template function #3921
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--- | ||
title: float | ||
linktitle: float | ||
description: Creates a `float` from the argument passed into the function. | ||
godocref: | ||
date: 2017-09-28 | ||
publishdate: 2017-09-28 | ||
lastmod: 2017-09-28 | ||
categories: [functions] | ||
menu: | ||
docs: | ||
parent: "functions" | ||
keywords: [strings,floats] | ||
signature: ["float INPUT"] | ||
workson: [] | ||
hugoversion: | ||
relatedfuncs: [] | ||
deprecated: false | ||
aliases: [] | ||
--- | ||
|
||
Useful for turning strings into floating point numbers. | ||
|
||
``` | ||
{{ float "1.23" }} → 1.23 | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,3 +81,40 @@ func TestToString(t *testing.T) { | |
assert.Equal(t, test.expect, result, errMsg) | ||
} | ||
} | ||
|
||
func TestToFloat(t *testing.T) { | ||
t.Parallel() | ||
|
||
ns := New() | ||
|
||
for i, test := range []struct { | ||
v interface{} | ||
expect interface{} | ||
}{ | ||
{"1", 1.0}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These tests assumes that the input is strings only. I suspect your implementation will fail on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've pushed additional additional test cases, they all pass. PS: Never mind me, there seems to be a difference between running PPS: I just commited a typo, nvm There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea, I see now -- I read your original code slightly wrong, but it helped with the test cases. |
||
{template.HTML("2"), 2.0}, | ||
{template.CSS("3"), 3.0}, | ||
{template.HTMLAttr("4"), 4.0}, | ||
{template.JS("-5.67"), -5.67}, | ||
{template.JSStr("6"), 6.0}, | ||
{"1.23", 1.23}, | ||
{"-1.23", -1.23}, | ||
{"0", 0.0}, | ||
{float64(2.12), 2.12}, | ||
{int64(123), 123.0}, | ||
{2, 2.0}, | ||
{t, false}, | ||
} { | ||
errMsg := fmt.Sprintf("[%d] %v", i, test.v) | ||
|
||
result, err := ns.ToFloat(test.v) | ||
|
||
if b, ok := test.expect.(bool); ok && !b { | ||
require.Error(t, err, errMsg) | ||
continue | ||
} | ||
|
||
require.NoError(t, err, errMsg) | ||
assert.Equal(t, test.expect, result, errMsg) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't change exisiting methods. This doesn't look correct and I'm surprised there were not red tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not? Should I just copy-and-paste the code that was used in toInt? I just took what was there before (the switch-case for the template types) and moved it into a separate helper function.