Skip to content
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
22 changes: 7 additions & 15 deletions bindings/c/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,15 @@ make test
```

```text
[==========] Running 5 tests from 1 test suite.
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 5 tests from OpendalBddTest
[ RUN ] OpendalBddTest.Write
[ OK ] OpendalBddTest.Write (4 ms)
[ RUN ] OpendalBddTest.Exist
[ OK ] OpendalBddTest.Exist (0 ms)
[ RUN ] OpendalBddTest.EntryMode
[ OK ] OpendalBddTest.EntryMode (0 ms)
[ RUN ] OpendalBddTest.ContentLength
[ OK ] OpendalBddTest.ContentLength (0 ms)
[ RUN ] OpendalBddTest.Read
[ OK ] OpendalBddTest.Read (0 ms)
[----------] 5 tests from OpendalBddTest (4 ms total)
[----------] 1 test from OpendalBddTest
[ RUN ] OpendalBddTest.FeatureTest
[ OK ] OpendalBddTest.FeatureTest (0 ms)
[----------] 1 test from OpendalBddTest (0 ms total)

[----------] Global test environment tear-down
[==========] 5 tests from 1 test suite ran. (4 ms total)
[ PASSED ] 5 tests.
[==========] 1 test from 1 test suite ran. (0 ms total)
[ PASSED ] 1 test.
```

131 changes: 51 additions & 80 deletions bindings/c/tests/bdd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,97 +25,68 @@ extern "C" {

class OpendalBddTest : public ::testing::Test {
protected:
// Setup code for the fixture
opendal_operator_ptr p;
std::string scheme;
std::string path;
std::string content;
opendal_operator_ptr p;
std::string scheme;
std::string path;
std::string content;

// the fixture setup is an operator write, which will be
// run at the beginning of every tests
void SetUp() override {
// construct the memory operator
this->scheme = std::string("memory");
this->path = std::string("test");
this->content = std::string("Hello, World!");
void SetUp() override
{
this->scheme = std::string("memory");
this->path = std::string("test");
this->content = std::string("Hello, World!");

// set options
opendal_operator_options options = opendal_operator_options_new();
opendal_operator_options_set(&options, "root", "/myroot");
opendal_operator_options options = opendal_operator_options_new();
opendal_operator_options_set(&options, "root", "/myroot");

this->p = opendal_operator_new(scheme.c_str(), options);
// Given A new OpenDAL Blocking Operator
this->p = opendal_operator_new(scheme.c_str(), options);
EXPECT_TRUE(this->p);

// free the options right away since the options is not used later on
opendal_operator_options_free(&options);
opendal_operator_options_free(&options);
}

EXPECT_TRUE(this->p);
void TearDown() override { opendal_operator_free(&this->p); }
};

// Scenario: OpenDAL Blocking Operations
TEST_F(OpendalBddTest, FeatureTest)
{
// When Blocking write path "test" with content "Hello, World!"
const opendal_bytes data = {
.data = (uint8_t *)this->content.c_str(),
.data = (uint8_t*)this->content.c_str(),
.len = this->content.length(),
};

opendal_code code =
opendal_operator_blocking_write(this->p, this->path.c_str(), data);

opendal_code code = opendal_operator_blocking_write(this->p, this->path.c_str(), data);
EXPECT_EQ(code, OPENDAL_OK);
}

// Teardown code for the fixture, free the operator
void TearDown() override { opendal_operator_free(&this->p); }
};

// do nothing, the fixture does the Write Test
TEST_F(OpendalBddTest, Write) {}

// The path must exist
TEST_F(OpendalBddTest, Exist) {
opendal_result_is_exist r =
opendal_operator_is_exist(this->p, this->path.c_str());

EXPECT_EQ(r.code, OPENDAL_OK);
EXPECT_TRUE(r.is_exist);
}

// The entry mode must be file
TEST_F(OpendalBddTest, EntryMode) {
opendal_result_stat r = opendal_operator_stat(this->p, this->path.c_str());
EXPECT_EQ(r.code, OPENDAL_OK);

opendal_metadata meta = r.meta;
EXPECT_TRUE(opendal_metadata_is_file(&meta));

opendal_metadata_free(&meta);
}

// The content length must be consistent
TEST_F(OpendalBddTest, ContentLength) {
opendal_result_stat r = opendal_operator_stat(this->p, this->path.c_str());
EXPECT_EQ(r.code, OPENDAL_OK);

opendal_metadata meta = r.meta;
EXPECT_EQ(opendal_metadata_content_length(&meta), 13);

opendal_metadata_free(&meta);
}

// We must read the correct content
TEST_F(OpendalBddTest, Read) {
struct opendal_result_read r =
opendal_operator_blocking_read(this->p, this->path.c_str());

EXPECT_EQ(r.code, OPENDAL_OK);
EXPECT_EQ(r.data->len, this->content.length());

for (int i = 0; i < r.data->len; i++) {
EXPECT_EQ(this->content[i], (char)(r.data->data[i]));
}

// free the bytes's heap memory
opendal_bytes_free(r.data);
// The blocking file "test" should exist
opendal_result_is_exist e = opendal_operator_is_exist(this->p, this->path.c_str());
EXPECT_EQ(e.code, OPENDAL_OK);
EXPECT_TRUE(e.is_exist);

// The blocking file "test" entry mode must be file
opendal_result_stat s = opendal_operator_stat(this->p, this->path.c_str());
EXPECT_EQ(s.code, OPENDAL_OK);
opendal_metadata meta = s.meta;
EXPECT_TRUE(opendal_metadata_is_file(&meta));

// The blocking file "test" content length must be 13
EXPECT_EQ(opendal_metadata_content_length(&meta), 13);
opendal_metadata_free(&meta);

// The blocking file "test" must have content "Hello, World!"
struct opendal_result_read r = opendal_operator_blocking_read(this->p, this->path.c_str());
EXPECT_EQ(r.code, OPENDAL_OK);
EXPECT_EQ(r.data->len, this->content.length());
for (int i = 0; i < r.data->len; i++) {
EXPECT_EQ(this->content[i], (char)(r.data->data[i]));
}
opendal_bytes_free(r.data);
}

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}