-
Notifications
You must be signed in to change notification settings - Fork 478
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bindings/C): support BDD tests using GTest (#2254)
* basic framework for BDD tests in Gtest Signed-off-by: Ji-Xinyou <jerryji0414@outlook.com> * remove basicio.c, replace with bdd test equivalent GTest Signed-off-by: Ji-Xinyou <jerryji0414@outlook.com> * some comments Signed-off-by: Ji-Xinyou <jerryji0414@outlook.com> * makefile minor change Signed-off-by: Ji-Xinyou <jerryji0414@outlook.com> * header Signed-off-by: Ji-Xinyou <jerryji0414@outlook.com> * remove gtest headers Signed-off-by: Ji-Xinyou <jerryji0414@outlook.com> * minor Signed-off-by: Ji-Xinyou <jerryji0414@outlook.com> * minor Signed-off-by: Ji-Xinyou <jerryji0414@outlook.com> * header Signed-off-by: Ji-Xinyou <jerryji0414@outlook.com> * gha Signed-off-by: Ji-Xinyou <jerryji0414@outlook.com> * gha Signed-off-by: Ji-Xinyou <jerryji0414@outlook.com> * manually setup c/cpp Signed-off-by: Ji-Xinyou <jerryji0414@outlook.com> * run test and fix build Signed-off-by: Ji-Xinyou <jerryji0414@outlook.com> * remove cpp setup, run test on make test Signed-off-by: Ji-Xinyou <jerryji0414@outlook.com> * minor Signed-off-by: Ji-Xinyou <jerryji0414@outlook.com> --------- Signed-off-by: Ji-Xinyou <jerryji0414@outlook.com>
- Loading branch information
1 parent
8ef38ac
commit 5d89060
Showing
4 changed files
with
190 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
name: Bindings C CI | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
tags: | ||
- '*' | ||
pull_request: | ||
branches: | ||
- main | ||
paths: | ||
- "bindings/c/**" | ||
- ".github/workflows/bindings_c.yml" | ||
workflow_dispatch: | ||
|
||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }} | ||
cancel-in-progress: true | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Install gtest manually | ||
run: | | ||
sudo apt-get install libgtest-dev | ||
cd /usr/src/gtest | ||
sudo cmake CMakeLists.txt | ||
sudo make | ||
sudo cp lib/*.a /usr/lib | ||
sudo ln -s /usr/lib/libgtest.a /usr/local/lib/libgtest.a | ||
sudo ln -s /usr/lib/libgtest_main.a /usr/local/lib/libgtest_main.a | ||
- name: Setup Rust toolchain | ||
uses: ./.github/actions/setup | ||
- name: Build C binding | ||
working-directory: "bindings/c" | ||
run: make build | ||
- name: Build and Run BDD tests | ||
working-directory: "bindings/c" | ||
run: make test | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
#include "gtest/gtest.h" | ||
|
||
extern "C" { | ||
#include "opendal.h" | ||
} | ||
|
||
class OpendalBddTest : public ::testing::Test { | ||
protected: | ||
// Setup code for the fixture | ||
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!"); | ||
|
||
this->p = opendal_operator_new(scheme.c_str()); | ||
|
||
EXPECT_TRUE(this->p); | ||
|
||
const opendal_bytes data = { | ||
.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); | ||
|
||
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); | ||
} | ||
|
||
int main(int argc, char** argv) | ||
{ | ||
::testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} |