Skip to content

Add missing imports to core test suite #3781

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

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions tests/core/flags/test_core_flags.odin
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,13 @@ test_if_map_cstrings_get_freed :: proc(t: ^testing.T) {

@(test)
test_os_handle :: proc(t: ^testing.T) {
defer if !testing.failed(t) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be

defer {
    if !testing.failed(t) {
        ...
    }
}

right?

Currently it will check if the test failed at the beginning of the test, which is always false.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be a quirk of the syntax, but I did test this before.

package deferred

import "core:fmt"

main :: proc() {
	a := false
	defer if a {
		fmt.println("Hellope.")
	}
	a = true
}

This prints for me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a discussion for this since it seems like something that could be confusing. #3782

Copy link
Collaborator

@laytan laytan Jun 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh strange I could've sworn it was a thing used to defer something IF the condition is currently true.
Because

if true {
    defer blah()
}

Would execute blah at the end of that if block, not at the end of the wanted scope.

I really thought defer if was created to get around that, @gingerBill ?

I guess I got the wrong impression somewhere and it really is just deferring the if statement.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did a quick peek through git log for defer if and all I could find was 118ab60 by @Kelimion.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But mainly I wrote it out that way because just by reading left to right we can rephrase the Odin code into English that highlights we want defer if (deferred conditional), not if defer (conditional defer) if we're unsure which of the two we should write.

That there's the confusion to begin with and that the 2nd phrasing is nonsensical in practice suggests it should be made an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't get if defer a { /* ... */ } to compile, if this was meant as a concrete and not a hypothetical syntax example.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed. Well, nothing to worry about then.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we all meant if x do defer ... here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I tried if a defer and it doesn't compile.

// Delete the file now that we're done.
//
// This is not done all the time, just in case the file is useful to debugging.
testing.expect_value(t, os.remove(TEMPORARY_FILENAME), os.ERROR_NONE)
}

TEMPORARY_FILENAME :: "test_core_flags_write_test_output_data"

test_data := "Hellope!"
Expand Down Expand Up @@ -1147,14 +1154,6 @@ test_os_handle :: proc(t: ^testing.T) {
testing.expect_value(t, read_ok, true)
file_contents_equal := 0 == bytes.compare(transmute([]u8)test_data, data)
testing.expectf(t, file_contents_equal, "expected file contents to be the same, got %v", data)

if file_contents_equal {
// Delete the file now that we're done.
//
// This is not done as a defer or all the time, just in case the file
// is useful to debugging.
testing.expect_value(t, os.remove(TEMPORARY_FILENAME), os.ERROR_NONE)
}
}

@(test)
Expand Down
2 changes: 2 additions & 0 deletions tests/core/normal.odin
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ download_assets :: proc() {
@(require) import "encoding/json"
@(require) import "encoding/varint"
@(require) import "encoding/xml"
@(require) import "flags"
@(require) import "fmt"
@(require) import "math"
@(require) import "math/big"
Expand All @@ -37,3 +38,4 @@ download_assets :: proc() {
@(require) import "text/match"
@(require) import "thread"
@(require) import "time"
@(require) import "unicode"