Skip to content
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

Add DefaultFormatter.EnableScientific #190

Closed
gavv opened this issue Dec 5, 2022 · 25 comments
Closed

Add DefaultFormatter.EnableScientific #190

gavv opened this issue Dec 5, 2022 · 25 comments
Assignees
Labels
feature New feature or request good first issue Good for newcomers help wanted Contributions are welcome
Milestone

Comments

@gavv
Copy link
Owner

gavv commented Dec 5, 2022

Add flag EnableScientific to DefaultFormatter that enables printing floats in scientific form. Without this flag, floats should be always printed in non-scientific form.

See also #160 and #159.

@gavv gavv added feature New feature or request help wanted Contributions are welcome good first issue Good for newcomers labels Dec 5, 2022
@rafiramadhana
Copy link
Contributor

hi @gavv , can I work on this? thanks

@gavv
Copy link
Owner Author

gavv commented Dec 14, 2022

Sure!

@rafiramadhana
Copy link
Contributor

thanks

will make progress within this week

@rafiramadhana
Copy link
Contributor

hi @gavv

in httpexpect we have this formatFloat function, which is called by filDelta:

data.Delta = formatFloat(failure.Delta.Value)

question: is the scope of this change only within "fillDelta that calls formatFloat"? or it could be bigger?

because it looks like formatRange and isNumber also involves float64 type

thanks

@gavv
Copy link
Owner Author

gavv commented Dec 17, 2022

Good question.

formatFloat is used only for printing delta when use use assertions like EqualDelta. This feature is definitely broader than just printing deltas, it should apply to actual and expected values in all assertion types.

Actually I don't quite understand why I added formatFloat. It seems misleading, because most times it's not used for formatting floats..

I think we should remove formatFloat at all and instead use formatValue and teach formatValue to print floats as formatFloat does it.

Also, as you correctly noticed, numbers may be printed via formatRange. Let's rework formatRange so that it will use formatValue to format min and max; this way all float formatting logic will be in one place - in formatValue.

Similarly, we should rework formatTyped to use formatValue when value is a number.

@gavv
Copy link
Owner Author

gavv commented Dec 17, 2022

Also, after more thought, it seems that it's better to reverse the option: add DisableScientific instead of EnableScientific.

It would work as follows:

@rafiramadhana
Copy link
Contributor

rafiramadhana commented Dec 17, 2022

this way all float formatting logic will be in one place - in formatValue.

ok, noted

so formatValue will be the entry point for any formatting (integer, float, string, etc) CMIIW

@rafiramadhana
Copy link
Contributor

Also, after more thought, it seems that it's better to reverse the option: add DisableScientific instead of EnableScientific.

agree

btw it looks like we don't have unit test for DefaultFormatter (i think it is important to test the flags), or i am missing something?

@rafiramadhana
Copy link
Contributor

rafiramadhana commented Dec 17, 2022

most of the tests in formatter_test.go directly test the formatting (e.g formatValue and formatRange)

@rafiramadhana
Copy link
Contributor

rafiramadhana commented Dec 17, 2022

maybe we can test the DefaultFormatter.buildFormatData and assert the templateData

templateData := f.buildFormatData(ctx, failure)

IMO on the contrary...if we assert the formatTemplate, it might be tedious because perhaps there will be a lot of text from the template result

func (f *DefaultFormatter) formatTemplate(

wdyt?

@gavv
Copy link
Owner Author

gavv commented Dec 17, 2022

so formatValue will be the entry point for any formatting (integer, float, string, etc) CMIIW

Yes.

btw it looks like we don't have unit test for DefaultFormatter (i think it is important to test the flags), or i am missing something?

We have a basic test in TestE2EReportNames, and an issue for expanding them: #165

However they are e2e tests that cover formatter very shallow and indirectly (thought I still think they would be helpful anyway).

maybe we can test the DefaultFormatter.buildFormatData and assert the templateData

This sound like a very good idea, I didn't think about it. And such tests would be extra valuable since FormatData is part of public API and currently it's not covered at all.

@rafiramadhana
Copy link
Contributor

However they are e2e tests that cover formatter very shallow and indirectly (thought I still think they would be helpful anyway).

it looks like #165 idea is (quite) similar with asserting the formatTemplate

yea, maybe asserting the formatTemplate is preferable in e2e because that is what our user see (the formatted text)

@rafiramadhana
Copy link
Contributor

rafiramadhana commented Dec 17, 2022

ok, i think i will do these changes in order

  1. update formatValue to be the entry point for any formatting (integer, float, string, etc)
  2. add unit test implementation (perhaps using the templateData)
  3. implement DisableScientific
  4. re-evaluate unit test implementation (let's see if we found a better way to do assertion on this)

i will start the PR(s) as draft

thanks

@gavv
Copy link
Owner Author

gavv commented Dec 17, 2022

Great, sounds good.

Yes, I think it makes sense to add both set of tests - described in #165 that match resulting text, and what you described here that match FormatData.

First set of tests will check the end result that is presented to user and will cover template formatter as well. Second will be more low-level and more granular, and wont bother about templates.

After completing this issue we may create a new one for adding more formatter unit tests based on FormatData.

@rafiramadhana
Copy link
Contributor

got it

@rafiramadhana rafiramadhana mentioned this issue Dec 18, 2022
3 tasks
@rafiramadhana
Copy link
Contributor

rafiramadhana commented Dec 31, 2022

@gavv Hi, I just realised this.

How do we pass the DisabledScientific flag from DefaultFormatter to the formatValue?

type DefaultFormatter struct {
	// If true, format float in non-scientific notation
	DisableScientific bool

Maybe, we can add a new parameter to formatValue

Something like:

type formatValueConfig struct {
    DisableScientific bool
}

func formatValue(cfg formatValueConfig, value interface{}) string {

Btw, this idea is referencing this statement:

this way all float formatting logic will be in one place - in formatValue.

#190 (comment)

Wdyt? Thanks

@gavv
Copy link
Owner Author

gavv commented Dec 31, 2022

We could make all formatXxx methods of formatter.

Why:

  • they are used only in formatter
  • formatter itself is actually "formatting config"; it has no other fileds except configuration and doesn't have state

@gavv
Copy link
Owner Author

gavv commented Dec 31, 2022

Happy new year btw :)

@rafiramadhana
Copy link
Contributor

rafiramadhana commented Dec 31, 2022

ok, it looks like that would result to a lot of changes

but it is ok because we have the time

i will create a draft PR first, consisting:

  • update all formatXXX to be a method of DefaultFormatter
  • add unit test related to DisableScientific

@rafiramadhana
Copy link
Contributor

happy new year to all of httpexpect contributors 🚀

@rafiramadhana
Copy link
Contributor

This issue needs additional work of adding unit test

Complete unit test with other assertion types (e.g. formatting of AssertionList with number as mentioned in #220 (comment))

ref: #220 (comment)

@gavv
Copy link
Owner Author

gavv commented Feb 1, 2023

This issue needs additional work of adding unit test

Fixed in d9300b9

@gavv
Copy link
Owner Author

gavv commented Feb 1, 2023

Everything is merged

@gavv gavv closed this as completed Feb 1, 2023
@gavv
Copy link
Owner Author

gavv commented Feb 1, 2023

I've created a follow-up issue: #258

@gavv gavv added this to the v2 milestone Feb 1, 2023
@gavv
Copy link
Owner Author

gavv commented Feb 1, 2023

Two more follow-up issues: #262, #264

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request good first issue Good for newcomers help wanted Contributions are welcome
Projects
None yet
Development

No branches or pull requests

2 participants