Skip to content

Commit

Permalink
test package is no longer reliant on @examples repository (#789)
Browse files Browse the repository at this point in the history
  • Loading branch information
UebelAndre committed Jun 24, 2021
1 parent d0a8586 commit 11551b1
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 10 deletions.
12 changes: 3 additions & 9 deletions test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ load(

rule_test(
name = "hello_lib_rule_test",
generates = ["libhello_lib--1343350674.rlib"],
rule = "@examples//hello_lib:hello_lib",
generates = ["libhello_lib--145651613.rlib"],
rule = "//test/rust:hello_lib",
)

rule_test(
Expand All @@ -15,11 +15,5 @@ rule_test(
"//rust/platform:windows": ["hello_world.exe"],
"//conditions:default": ["hello_world"],
}),
rule = "@examples//hello_world:hello_world",
)

rule_test(
name = "greeting_rule_test",
generates = ["libhello_lib--1343350674.rlib"],
rule = "@examples//hello_lib:hello_lib",
rule = "//test/rust:hello_world",
)
17 changes: 17 additions & 0 deletions test/rust/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library")

package(default_visibility = ["//visibility:public"])

rust_library(
name = "hello_lib",
srcs = [
"src/greeter.rs",
"src/lib.rs",
],
)

rust_binary(
name = "hello_world",
srcs = ["src/main.rs"],
deps = [":hello_lib"],
)
75 changes: 75 additions & 0 deletions test/rust/src/greeter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2015 The Bazel Authors. All rights reserved.
//
// Licensed 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.

/// Object that displays a greeting.
pub struct Greeter {
greeting: String,
}

/// Implementation of Greeter.
impl Greeter {
/// Constructs a new `Greeter`.
///
/// # Examples
///
/// ```
/// use hello_lib::greeter::Greeter;
///
/// let greeter = Greeter::new("Hello");
/// ```
pub fn new(greeting: &str) -> Greeter {
Greeter {
greeting: greeting.to_string(),
}
}

/// Returns the greeting as a string.
///
/// # Examples
///
/// ```
/// use hello_lib::greeter::Greeter;
///
/// let greeter = Greeter::new("Hello");
/// let greeting = greeter.greeting("World");
/// ```
pub fn greeting(&self, thing: &str) -> String {
format!("{} {}", &self.greeting, thing)
}

/// Prints the greeting.
///
/// # Examples
///
/// ```
/// use hello_lib::greeter::Greeter;
///
/// let greeter = Greeter::new("Hello");
/// greeter.greet("World");
/// ```
pub fn greet(&self, thing: &str) {
println!("{} {}", &self.greeting, thing);
}
}

#[cfg(test)]
mod test {
use super::Greeter;

#[test]
fn test_greeting() {
let hello = Greeter::new("Hi");
assert_eq!("Hi Rust", hello.greeting("Rust"));
}
}
15 changes: 15 additions & 0 deletions test/rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2015 The Bazel Authors. All rights reserved.
//
// Licensed 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.

pub mod greeter;
22 changes: 22 additions & 0 deletions test/rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2015 The Bazel Authors. All rights reserved.
//
// Licensed 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.

extern crate hello_lib;

use hello_lib::greeter;

fn main() {
let hello = greeter::Greeter::new("Hello");
hello.greet("world");
}
1 change: 0 additions & 1 deletion test/rustc_env_files/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ rust_binary(
name = "hello_env",
srcs = ["src/main.rs"],
rustc_env_files = [":generate_rustc_env_file"],
deps = ["@examples//hello_lib"],
)

genrule(
Expand Down

0 comments on commit 11551b1

Please sign in to comment.